###
###
### Clark, Zucker, and Urpelainen; Renewable and Sustainable Energy Reviews
### The Future of Coal-Fired Power Generation in Southeast Asia
###
### Replication file: parameter-by-parameter variation
### Created 17 Dec 2019
###
###

####### BUILD EACH SCENARIO INDIVIDUALLY. EMPTY ENVIRONMENT AFTER EACH RUN.

pacman::p_load(dplyr, knitr, kableExtra, ggplot2, gridExtra, readr, ggrepel, reshape2, xtable, grid)

setwd("~/Dropbox/SE Asia Coal/Data")

#### SETUP ####

secoal_orig <- read.csv("SEAsia_CoalPlants.csv")
secoal_orig <- secoal_orig[secoal_orig$region == "SE Asia" & secoal_orig$status != "retired",]
secoal <- secoal_orig[,c("capacity","status","country","commission_year")]

secoal_cyear <- read.csv("coal_cyear_seasia.csv")

status_percentages <- readRDS("status_percentages.rds")

secoal$commission_year <- as.numeric(as.character(secoal$commission_year))

capfact <- data.frame(country = c("Vietnam","Indonesia","Malaysia","Philippines","Mean"),
                      low = c(53,51,51,55,NA), high = c(70,74,74,78,NA),
                      mid = c(61.5,62.5,58,66.5,NA))
capfact$low[5] <- mean(capfact$low[1:4])
capfact$high[5] <- mean(capfact$high[1:4])
capfact$mid[5] <- mean(capfact$mid[1:4])
capfact[,2:4] <- capfact[,2:4]/100

rownames(capfact) <- capfact$country

#### PARAM. SET 1 (BASELINE) ####

lifespan <- 35 # operational lifespan for individual units

projspan <- 2017:2057 # year span for projections

attrition_specificity <- 1 # 0: region-wide attrition rate, 1: country-specific

attrition_scenario <- 1 # 1: middle attrition rate; 1<: high attrition rate; 1>: low attrition rate

cap_scenario <- "mid" # "low": low capacity factor; "mid": mid capacity factor; "high": high capacity factor

operating_year <- round(mean(secoal$commission_year, na.rm = T), 0) # est. commissioning year for operating units w/o a listed commissioning year

announced_time <- 10 # pipeline times
prepermit_time <- 7
permitted_time <- 4
construction_time <- 1

#### PARAM. SET 1 PROJECTIONS ####

projspan <- paste0("Y",projspan)
lastyear <- as.numeric(substr(last(projspan), 2, 5))
firstyear <- as.numeric(substr(first(projspan), 2, 5))

attr_All <- (status_percentages$All[3] + status_percentages$All[7])/
  (status_percentages$All[3] + status_percentages$All[7] + status_percentages$All[6])*attrition_scenario
attr_Cambodia <- (status_percentages$Cambodia[3] + status_percentages$Cambodia[7])/
  (status_percentages$Cambodia[3] + status_percentages$Cambodia[7] + status_percentages$Cambodia[6])*attrition_scenario
attr_Indonesia <- (status_percentages$Indonesia[3] + status_percentages$Indonesia[7])/
  (status_percentages$Indonesia[3] + status_percentages$Indonesia[7] + status_percentages$Indonesia[6])*attrition_scenario
attr_Laos <- (status_percentages$Laos[3] + status_percentages$Laos[7])/
  (status_percentages$Laos[3] + status_percentages$Laos[7] + status_percentages$Laos[6])*attrition_scenario
attr_Malaysia <- (status_percentages$Malaysia[3] + status_percentages$Malaysia[7])/
  (status_percentages$Malaysia[3] + status_percentages$Malaysia[7] + status_percentages$Malaysia[6])*attrition_scenario
attr_Myanmar <- (status_percentages$Myanmar[3] + status_percentages$Myanmar[7])/
  (status_percentages$Myanmar[3] + status_percentages$Myanmar[7] + status_percentages$Myanmar[6])*attrition_scenario
attr_Philippines <- (status_percentages$Philippines[3] + status_percentages$Philippines[7])/
  (status_percentages$Philippines[3] + status_percentages$Philippines[7] + status_percentages$Philippines[6])*attrition_scenario
attr_Thailand <- (status_percentages$Thailand[3] + status_percentages$Thailand[7])/
  (status_percentages$Thailand[3] + status_percentages$Thailand[7] + status_percentages$Thailand[6])*attrition_scenario
attr_Vietnam <- (status_percentages$Vietnam[3] + status_percentages$Vietnam[7])/
  (status_percentages$Vietnam[3] + status_percentages$Vietnam[7] + status_percentages$Vietnam[6])*attrition_scenario

attr_All <- ifelse(attr_All > 1, 1, attr_All)
attr_Cambodia <- ifelse(attr_Cambodia > 1, 1, attr_Cambodia)
attr_Indonesia <- ifelse(attr_Indonesia > 1, 1, attr_Indonesia)
attr_Laos <- ifelse(attr_Laos > 1, 1, attr_Laos)
attr_Malaysia <- ifelse(attr_Malaysia > 1, 1, attr_Malaysia)
attr_Myanmar <- ifelse(attr_Myanmar > 1, 1, attr_Myanmar)
attr_Philippines <- ifelse(attr_Philippines > 1, 1, attr_Philippines)
attr_Thailand <- ifelse(attr_Thailand > 1, 1, attr_Thailand)
attr_Vietnam <- ifelse(attr_Vietnam > 1, 1, attr_Vietnam)

if(attrition_specificity == 0){
  secoal$capacity[secoal$status != "operating" & !is.na(secoal$capacity)] <- secoal$capacity[
    secoal$status != "operating" & !is.na(secoal$capacity)]*(1-attr_All)
} else{
  for (i in 1:nrow(secoal)){
    secoal$capacity[i] <- ifelse(secoal$country[i] == "Cambodia" &
                                   secoal$status[i] != "operating",
                                 secoal$capacity[i]*(1-attr_Cambodia),
                                 ifelse(secoal$country[i] == "Indonesia" &
                                          secoal$status[i] != "operating",
                                        secoal$capacity[i]*(1-attr_Indonesia),
                                        ifelse(secoal$country[i] == "Laos" &
                                                 secoal$status[i] != "operating",
                                               secoal$capacity[i]*(1-attr_Laos),
                                               ifelse(secoal$country[i] == "Malaysia" &
                                                        secoal$status[i] != "operating",
                                                      secoal$capacity[i]*(1-attr_Malaysia),
                                                      ifelse(secoal$country[i] == "Myanmar" &
                                                               secoal$status[i] != "operating",
                                                             secoal$capacity[i]*(1-attr_Myanmar),
                                                             ifelse(secoal$country[i] == "Philippines" &
                                                                      secoal$status[i] != "operating",
                                                                    secoal$capacity[i]*(1-attr_Philippines),
                                                                    ifelse(secoal$country[i] == "Thailand" &
                                                                             secoal$status[i] != "operating",
                                                                           secoal$capacity[i]*(1-attr_Thailand),
                                                                           ifelse(secoal$country[i] == "Vietnam" &
                                                                                    secoal$status[i] != "operating",
                                                                                  secoal$capacity[i]*(1-attr_Vietnam),
                                                                                  secoal$capacity[i]))))))))
  }
}

secoal$commission_year[secoal$status == "announced"] <- 2017 + announced_time
secoal$commission_year[secoal$status == "pre-permit"] <- 2017 + prepermit_time
secoal$commission_year[secoal$status == "permitted"] <- 2017 + permitted_time
secoal$commission_year[secoal$status == "construction"] <- 2017 + construction_time
secoal$commission_year[secoal$status == "operating" & is.na(secoal$commission_year)] <- operating_year

secoal <- secoal[!is.na(secoal$commission_year),]

secoal$retirement_year <- as.numeric(as.character(secoal$commission_year)) + lifespan

secoal[projspan] <- NA

for (j in 6:ncol(secoal)){
  .year <- as.numeric(substr(colnames(secoal)[j],2,5))
  for (i in 1:nrow(secoal)){
    secoal[i,j] <- ifelse(.year >= secoal$commission_year[i] &
                            .year <= secoal$retirement_year[i],
                          1, 0)
  }
}

secoal[,c(6:ncol(secoal))] <- sapply(secoal[,c(6:ncol(secoal))],
                                     FUN = function(x){secoal$capacity * x})
secoal[secoal$country == "Cambodia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Cambodia",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Indonesia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Indonesia",c(6:ncol(secoal))],
                                                                  FUN = function(x){capfact["Indonesia",cap_scenario] * x})
secoal[secoal$country == "Laos",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Laos",c(6:ncol(secoal))],
                                                             FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Malaysia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Malaysia",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Malaysia",cap_scenario] * x})
secoal[secoal$country == "Myanmar",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Myanmar",c(6:ncol(secoal))],
                                                                FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Philippines",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Philippines",c(6:ncol(secoal))],
                                                                    FUN = function(x){capfact["Philippines",cap_scenario] * x})
secoal[secoal$country == "Thailand",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Thailand",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Vietnam",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Vietnam",c(6:ncol(secoal))],
                                                                FUN = function(x){capfact["Vietnam",cap_scenario] * x})

all_proj <- data.frame(year = as.numeric(substr(projspan,2,5)), capacity = colSums(secoal[,6:ncol(secoal)]))
cambodia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Cambodia",6:ncol(secoal)]))
indonesia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                             capacity = colSums(secoal[secoal$country == "Indonesia",6:ncol(secoal)]))
laos_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                        capacity = colSums(secoal[secoal$country == "Laos",6:ncol(secoal)]))
malaysia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Malaysia",6:ncol(secoal)]))
myanmar_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                           capacity = colSums(secoal[secoal$country == "Myanmar",6:ncol(secoal)]))
philippines_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                               capacity = colSums(secoal[secoal$country == "Philippines",6:ncol(secoal)]))
thailand_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Thailand",6:ncol(secoal)]))
vietnam_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                           capacity = colSums(secoal[secoal$country == "Vietnam",6:ncol(secoal)]))

projectiondf <- data.frame(Year = c(firstyear,
                                    round((lastyear - firstyear)/4, 0) + firstyear,2*round((lastyear - firstyear)/4, 0) + firstyear,3*round((lastyear - firstyear)/4, 0) + firstyear,lastyear),
                           All = c(all_proj$capacity[all_proj$year == firstyear],
                                   all_proj$capacity[all_proj$year == round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == lastyear]),
                           Indonesia = c(indonesia_proj$capacity[indonesia_proj$year == firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == lastyear]),
                           Malaysia = c(malaysia_proj$capacity[malaysia_proj$year == firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == lastyear]),
                           Philippines = c(philippines_proj$capacity[philippines_proj$year == firstyear],
                                           philippines_proj$capacity[philippines_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == lastyear]),
                           Vietnam = c(vietnam_proj$capacity[vietnam_proj$year == firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == lastyear]))

projectiondf[,c(2:6)] <- round(projectiondf[,c(2:6)], 0)

countrylist <- c("All","Cambodia","Indonesia","Laos","Malaysia","Myanmar","Philippines","Thailand","Vietnam")
attrlist <- c(attr_All,attr_Cambodia,attr_Indonesia,attr_Laos,attr_Malaysia,attr_Myanmar,
              attr_Philippines,attr_Thailand,attr_Vietnam)
attrtab <- data.frame(Country = countrylist, Attrition = attrlist)
attrtab$Attrition <- round(attrtab$Attrition, 2)

co2f <- 8.2245*1000 * 215.225 * 0.00000000397347

all_proj$country <- "SE Asia"
cambodia_proj$country <- "Cambodia"
indonesia_proj$country <- "Indonesia"
laos_proj$country <- "Laos"
malaysia_proj$country <- "Malaysia"
myanmar_proj$country <- "Myanmar"
philippines_proj$country <- "Philippines"
thailand_proj$country <- "Thailand"
vietnam_proj$country <- "Vietnam"

bycountry <- rbind(all_proj, cambodia_proj, indonesia_proj, laos_proj, malaysia_proj, philippines_proj,
                   thailand_proj, vietnam_proj)

bycountry$label <- bycountry$country
bycountry$label[bycountry$year != max(bycountry$year)-round((max(bycountry$year)-min(bycountry$year))/1.3,0)] <- ""

bycountry_co2 <- bycountry
bycountry_co2$capacity <- bycountry_co2$capacity * co2f
colnames(bycountry_co2)[2] <- "co2_yearly"

bycountry_co2$co2_cumulative <- bycountry_co2$co2_yearly

for (i in 2:nrow(bycountry_co2)){
  bycountry_co2$co2_cumulative[i] <- ifelse(bycountry_co2$country[i-1] == bycountry_co2$country[i],
                                            bycountry_co2$co2_cumulative[i-1] + bycountry_co2$co2_cumulative[i],
                                            bycountry_co2$co2_cumulative[i])
}

#### PARAM. SET 1 EXPORTS ####

saveRDS(projectiondf, "projectiondf_p-by-p_1.rds")
saveRDS(bycountry_co2, "co2_p-by-p_1.rds")

#### PARAM. SET 2 (HIGH CAPACITY FACTOR) ####

lifespan <- 35 # operational lifespan for individual units

projspan <- 2017:2057 # year span for projections

attrition_specificity <- 1 # 0: region-wide attrition rate, 1: country-specific

attrition_scenario <- 1 # 1: middle attrition rate; 1<: high attrition rate; 1>: low attrition rate

cap_scenario <- "high" # "low": low capacity factor; "mid": mid capacity factor; "high": high capacity factor

operating_year <- round(mean(secoal$commission_year, na.rm = T), 0) # est. commissioning year for operating units w/o a listed commissioning year

announced_time <- 10 # pipeline times
prepermit_time <- 7
permitted_time <- 4
construction_time <- 1

#### PARAM. SET 2 PROJECTIONS ####

projspan <- paste0("Y",projspan)
lastyear <- as.numeric(substr(last(projspan), 2, 5))
firstyear <- as.numeric(substr(first(projspan), 2, 5))

attr_All <- (status_percentages$All[3] + status_percentages$All[7])/
  (status_percentages$All[3] + status_percentages$All[7] + status_percentages$All[6])*attrition_scenario
attr_Cambodia <- (status_percentages$Cambodia[3] + status_percentages$Cambodia[7])/
  (status_percentages$Cambodia[3] + status_percentages$Cambodia[7] + status_percentages$Cambodia[6])*attrition_scenario
attr_Indonesia <- (status_percentages$Indonesia[3] + status_percentages$Indonesia[7])/
  (status_percentages$Indonesia[3] + status_percentages$Indonesia[7] + status_percentages$Indonesia[6])*attrition_scenario
attr_Laos <- (status_percentages$Laos[3] + status_percentages$Laos[7])/
  (status_percentages$Laos[3] + status_percentages$Laos[7] + status_percentages$Laos[6])*attrition_scenario
attr_Malaysia <- (status_percentages$Malaysia[3] + status_percentages$Malaysia[7])/
  (status_percentages$Malaysia[3] + status_percentages$Malaysia[7] + status_percentages$Malaysia[6])*attrition_scenario
attr_Myanmar <- (status_percentages$Myanmar[3] + status_percentages$Myanmar[7])/
  (status_percentages$Myanmar[3] + status_percentages$Myanmar[7] + status_percentages$Myanmar[6])*attrition_scenario
attr_Philippines <- (status_percentages$Philippines[3] + status_percentages$Philippines[7])/
  (status_percentages$Philippines[3] + status_percentages$Philippines[7] + status_percentages$Philippines[6])*attrition_scenario
attr_Thailand <- (status_percentages$Thailand[3] + status_percentages$Thailand[7])/
  (status_percentages$Thailand[3] + status_percentages$Thailand[7] + status_percentages$Thailand[6])*attrition_scenario
attr_Vietnam <- (status_percentages$Vietnam[3] + status_percentages$Vietnam[7])/
  (status_percentages$Vietnam[3] + status_percentages$Vietnam[7] + status_percentages$Vietnam[6])*attrition_scenario

attr_All <- ifelse(attr_All > 1, 1, attr_All)
attr_Cambodia <- ifelse(attr_Cambodia > 1, 1, attr_Cambodia)
attr_Indonesia <- ifelse(attr_Indonesia > 1, 1, attr_Indonesia)
attr_Laos <- ifelse(attr_Laos > 1, 1, attr_Laos)
attr_Malaysia <- ifelse(attr_Malaysia > 1, 1, attr_Malaysia)
attr_Myanmar <- ifelse(attr_Myanmar > 1, 1, attr_Myanmar)
attr_Philippines <- ifelse(attr_Philippines > 1, 1, attr_Philippines)
attr_Thailand <- ifelse(attr_Thailand > 1, 1, attr_Thailand)
attr_Vietnam <- ifelse(attr_Vietnam > 1, 1, attr_Vietnam)

if(attrition_specificity == 0){
  secoal$capacity[secoal$status != "operating" & !is.na(secoal$capacity)] <- secoal$capacity[
    secoal$status != "operating" & !is.na(secoal$capacity)]*(1-attr_All)
} else{
  for (i in 1:nrow(secoal)){
    secoal$capacity[i] <- ifelse(secoal$country[i] == "Cambodia" &
                                   secoal$status[i] != "operating",
                                 secoal$capacity[i]*(1-attr_Cambodia),
                                 ifelse(secoal$country[i] == "Indonesia" &
                                          secoal$status[i] != "operating",
                                        secoal$capacity[i]*(1-attr_Indonesia),
                                        ifelse(secoal$country[i] == "Laos" &
                                                 secoal$status[i] != "operating",
                                               secoal$capacity[i]*(1-attr_Laos),
                                               ifelse(secoal$country[i] == "Malaysia" &
                                                        secoal$status[i] != "operating",
                                                      secoal$capacity[i]*(1-attr_Malaysia),
                                                      ifelse(secoal$country[i] == "Myanmar" &
                                                               secoal$status[i] != "operating",
                                                             secoal$capacity[i]*(1-attr_Myanmar),
                                                             ifelse(secoal$country[i] == "Philippines" &
                                                                      secoal$status[i] != "operating",
                                                                    secoal$capacity[i]*(1-attr_Philippines),
                                                                    ifelse(secoal$country[i] == "Thailand" &
                                                                             secoal$status[i] != "operating",
                                                                           secoal$capacity[i]*(1-attr_Thailand),
                                                                           ifelse(secoal$country[i] == "Vietnam" &
                                                                                    secoal$status[i] != "operating",
                                                                                  secoal$capacity[i]*(1-attr_Vietnam),
                                                                                  secoal$capacity[i]))))))))
  }
}

secoal$commission_year[secoal$status == "announced"] <- 2017 + announced_time
secoal$commission_year[secoal$status == "pre-permit"] <- 2017 + prepermit_time
secoal$commission_year[secoal$status == "permitted"] <- 2017 + permitted_time
secoal$commission_year[secoal$status == "construction"] <- 2017 + construction_time
secoal$commission_year[secoal$status == "operating" & is.na(secoal$commission_year)] <- operating_year

secoal <- secoal[!is.na(secoal$commission_year),]

secoal$retirement_year <- as.numeric(as.character(secoal$commission_year)) + lifespan

secoal[projspan] <- NA

for (j in 6:ncol(secoal)){
  .year <- as.numeric(substr(colnames(secoal)[j],2,5))
  for (i in 1:nrow(secoal)){
    secoal[i,j] <- ifelse(.year >= secoal$commission_year[i] &
                            .year <= secoal$retirement_year[i],
                          1, 0)
  }
}

secoal[,c(6:ncol(secoal))] <- sapply(secoal[,c(6:ncol(secoal))],
                                     FUN = function(x){secoal$capacity * x})
secoal[secoal$country == "Cambodia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Cambodia",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Indonesia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Indonesia",c(6:ncol(secoal))],
                                                                  FUN = function(x){capfact["Indonesia",cap_scenario] * x})
secoal[secoal$country == "Laos",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Laos",c(6:ncol(secoal))],
                                                             FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Malaysia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Malaysia",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Malaysia",cap_scenario] * x})
secoal[secoal$country == "Myanmar",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Myanmar",c(6:ncol(secoal))],
                                                                FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Philippines",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Philippines",c(6:ncol(secoal))],
                                                                    FUN = function(x){capfact["Philippines",cap_scenario] * x})
secoal[secoal$country == "Thailand",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Thailand",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Vietnam",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Vietnam",c(6:ncol(secoal))],
                                                                FUN = function(x){capfact["Vietnam",cap_scenario] * x})

all_proj <- data.frame(year = as.numeric(substr(projspan,2,5)), capacity = colSums(secoal[,6:ncol(secoal)]))
cambodia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Cambodia",6:ncol(secoal)]))
indonesia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                             capacity = colSums(secoal[secoal$country == "Indonesia",6:ncol(secoal)]))
laos_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                        capacity = colSums(secoal[secoal$country == "Laos",6:ncol(secoal)]))
malaysia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Malaysia",6:ncol(secoal)]))
myanmar_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                           capacity = colSums(secoal[secoal$country == "Myanmar",6:ncol(secoal)]))
philippines_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                               capacity = colSums(secoal[secoal$country == "Philippines",6:ncol(secoal)]))
thailand_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Thailand",6:ncol(secoal)]))
vietnam_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                           capacity = colSums(secoal[secoal$country == "Vietnam",6:ncol(secoal)]))

projectiondf <- data.frame(Year = c(firstyear,
                                    round((lastyear - firstyear)/4, 0) + firstyear,2*round((lastyear - firstyear)/4, 0) + firstyear,3*round((lastyear - firstyear)/4, 0) + firstyear,lastyear),
                           All = c(all_proj$capacity[all_proj$year == firstyear],
                                   all_proj$capacity[all_proj$year == round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == lastyear]),
                           Indonesia = c(indonesia_proj$capacity[indonesia_proj$year == firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == lastyear]),
                           Malaysia = c(malaysia_proj$capacity[malaysia_proj$year == firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == lastyear]),
                           Philippines = c(philippines_proj$capacity[philippines_proj$year == firstyear],
                                           philippines_proj$capacity[philippines_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == lastyear]),
                           Vietnam = c(vietnam_proj$capacity[vietnam_proj$year == firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == lastyear]))

projectiondf[,c(2:6)] <- round(projectiondf[,c(2:6)], 0)

countrylist <- c("All","Cambodia","Indonesia","Laos","Malaysia","Myanmar","Philippines","Thailand","Vietnam")
attrlist <- c(attr_All,attr_Cambodia,attr_Indonesia,attr_Laos,attr_Malaysia,attr_Myanmar,
              attr_Philippines,attr_Thailand,attr_Vietnam)
attrtab <- data.frame(Country = countrylist, Attrition = attrlist)
attrtab$Attrition <- round(attrtab$Attrition, 2)

co2f <- 8.2245*1000 * 215.225 * 0.00000000397347

all_proj$country <- "SE Asia"
cambodia_proj$country <- "Cambodia"
indonesia_proj$country <- "Indonesia"
laos_proj$country <- "Laos"
malaysia_proj$country <- "Malaysia"
myanmar_proj$country <- "Myanmar"
philippines_proj$country <- "Philippines"
thailand_proj$country <- "Thailand"
vietnam_proj$country <- "Vietnam"

bycountry <- rbind(all_proj, cambodia_proj, indonesia_proj, laos_proj, malaysia_proj, philippines_proj,
                   thailand_proj, vietnam_proj)

bycountry$label <- bycountry$country
bycountry$label[bycountry$year != max(bycountry$year)-round((max(bycountry$year)-min(bycountry$year))/1.3,0)] <- ""

bycountry_co2 <- bycountry
bycountry_co2$capacity <- bycountry_co2$capacity * co2f
colnames(bycountry_co2)[2] <- "co2_yearly"

bycountry_co2$co2_cumulative <- bycountry_co2$co2_yearly

for (i in 2:nrow(bycountry_co2)){
  bycountry_co2$co2_cumulative[i] <- ifelse(bycountry_co2$country[i-1] == bycountry_co2$country[i],
                                            bycountry_co2$co2_cumulative[i-1] + bycountry_co2$co2_cumulative[i],
                                            bycountry_co2$co2_cumulative[i])
}

#### PARAM. SET 2 EXPORTS ####

saveRDS(projectiondf, "projectiondf_p-by-p_2.rds")
saveRDS(bycountry_co2, "co2_p-by-p_2.rds")

#### PARAM. SET 3 (LOW CAPACITY FACTOR) ####

lifespan <- 35 # operational lifespan for individual units

projspan <- 2017:2057 # year span for projections

attrition_specificity <- 1 # 0: region-wide attrition rate, 1: country-specific

attrition_scenario <- 1 # 1: middle attrition rate; 1<: high attrition rate; 1>: low attrition rate

cap_scenario <- "low" # "low": low capacity factor; "mid": mid capacity factor; "high": high capacity factor

operating_year <- round(mean(secoal$commission_year, na.rm = T), 0) # est. commissioning year for operating units w/o a listed commissioning year

announced_time <- 10 # pipeline times
prepermit_time <- 7
permitted_time <- 4
construction_time <- 1

#### PARAM. SET 3 PROJECTIONS ####

projspan <- paste0("Y",projspan)
lastyear <- as.numeric(substr(last(projspan), 2, 5))
firstyear <- as.numeric(substr(first(projspan), 2, 5))

attr_All <- (status_percentages$All[3] + status_percentages$All[7])/
  (status_percentages$All[3] + status_percentages$All[7] + status_percentages$All[6])*attrition_scenario
attr_Cambodia <- (status_percentages$Cambodia[3] + status_percentages$Cambodia[7])/
  (status_percentages$Cambodia[3] + status_percentages$Cambodia[7] + status_percentages$Cambodia[6])*attrition_scenario
attr_Indonesia <- (status_percentages$Indonesia[3] + status_percentages$Indonesia[7])/
  (status_percentages$Indonesia[3] + status_percentages$Indonesia[7] + status_percentages$Indonesia[6])*attrition_scenario
attr_Laos <- (status_percentages$Laos[3] + status_percentages$Laos[7])/
  (status_percentages$Laos[3] + status_percentages$Laos[7] + status_percentages$Laos[6])*attrition_scenario
attr_Malaysia <- (status_percentages$Malaysia[3] + status_percentages$Malaysia[7])/
  (status_percentages$Malaysia[3] + status_percentages$Malaysia[7] + status_percentages$Malaysia[6])*attrition_scenario
attr_Myanmar <- (status_percentages$Myanmar[3] + status_percentages$Myanmar[7])/
  (status_percentages$Myanmar[3] + status_percentages$Myanmar[7] + status_percentages$Myanmar[6])*attrition_scenario
attr_Philippines <- (status_percentages$Philippines[3] + status_percentages$Philippines[7])/
  (status_percentages$Philippines[3] + status_percentages$Philippines[7] + status_percentages$Philippines[6])*attrition_scenario
attr_Thailand <- (status_percentages$Thailand[3] + status_percentages$Thailand[7])/
  (status_percentages$Thailand[3] + status_percentages$Thailand[7] + status_percentages$Thailand[6])*attrition_scenario
attr_Vietnam <- (status_percentages$Vietnam[3] + status_percentages$Vietnam[7])/
  (status_percentages$Vietnam[3] + status_percentages$Vietnam[7] + status_percentages$Vietnam[6])*attrition_scenario

attr_All <- ifelse(attr_All > 1, 1, attr_All)
attr_Cambodia <- ifelse(attr_Cambodia > 1, 1, attr_Cambodia)
attr_Indonesia <- ifelse(attr_Indonesia > 1, 1, attr_Indonesia)
attr_Laos <- ifelse(attr_Laos > 1, 1, attr_Laos)
attr_Malaysia <- ifelse(attr_Malaysia > 1, 1, attr_Malaysia)
attr_Myanmar <- ifelse(attr_Myanmar > 1, 1, attr_Myanmar)
attr_Philippines <- ifelse(attr_Philippines > 1, 1, attr_Philippines)
attr_Thailand <- ifelse(attr_Thailand > 1, 1, attr_Thailand)
attr_Vietnam <- ifelse(attr_Vietnam > 1, 1, attr_Vietnam)

if(attrition_specificity == 0){
  secoal$capacity[secoal$status != "operating" & !is.na(secoal$capacity)] <- secoal$capacity[
    secoal$status != "operating" & !is.na(secoal$capacity)]*(1-attr_All)
} else{
  for (i in 1:nrow(secoal)){
    secoal$capacity[i] <- ifelse(secoal$country[i] == "Cambodia" &
                                   secoal$status[i] != "operating",
                                 secoal$capacity[i]*(1-attr_Cambodia),
                                 ifelse(secoal$country[i] == "Indonesia" &
                                          secoal$status[i] != "operating",
                                        secoal$capacity[i]*(1-attr_Indonesia),
                                        ifelse(secoal$country[i] == "Laos" &
                                                 secoal$status[i] != "operating",
                                               secoal$capacity[i]*(1-attr_Laos),
                                               ifelse(secoal$country[i] == "Malaysia" &
                                                        secoal$status[i] != "operating",
                                                      secoal$capacity[i]*(1-attr_Malaysia),
                                                      ifelse(secoal$country[i] == "Myanmar" &
                                                               secoal$status[i] != "operating",
                                                             secoal$capacity[i]*(1-attr_Myanmar),
                                                             ifelse(secoal$country[i] == "Philippines" &
                                                                      secoal$status[i] != "operating",
                                                                    secoal$capacity[i]*(1-attr_Philippines),
                                                                    ifelse(secoal$country[i] == "Thailand" &
                                                                             secoal$status[i] != "operating",
                                                                           secoal$capacity[i]*(1-attr_Thailand),
                                                                           ifelse(secoal$country[i] == "Vietnam" &
                                                                                    secoal$status[i] != "operating",
                                                                                  secoal$capacity[i]*(1-attr_Vietnam),
                                                                                  secoal$capacity[i]))))))))
  }
}

secoal$commission_year[secoal$status == "announced"] <- 2017 + announced_time
secoal$commission_year[secoal$status == "pre-permit"] <- 2017 + prepermit_time
secoal$commission_year[secoal$status == "permitted"] <- 2017 + permitted_time
secoal$commission_year[secoal$status == "construction"] <- 2017 + construction_time
secoal$commission_year[secoal$status == "operating" & is.na(secoal$commission_year)] <- operating_year

secoal <- secoal[!is.na(secoal$commission_year),]

secoal$retirement_year <- as.numeric(as.character(secoal$commission_year)) + lifespan

secoal[projspan] <- NA

for (j in 6:ncol(secoal)){
  .year <- as.numeric(substr(colnames(secoal)[j],2,5))
  for (i in 1:nrow(secoal)){
    secoal[i,j] <- ifelse(.year >= secoal$commission_year[i] &
                            .year <= secoal$retirement_year[i],
                          1, 0)
  }
}

secoal[,c(6:ncol(secoal))] <- sapply(secoal[,c(6:ncol(secoal))],
                                     FUN = function(x){secoal$capacity * x})
secoal[secoal$country == "Cambodia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Cambodia",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Indonesia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Indonesia",c(6:ncol(secoal))],
                                                                  FUN = function(x){capfact["Indonesia",cap_scenario] * x})
secoal[secoal$country == "Laos",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Laos",c(6:ncol(secoal))],
                                                             FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Malaysia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Malaysia",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Malaysia",cap_scenario] * x})
secoal[secoal$country == "Myanmar",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Myanmar",c(6:ncol(secoal))],
                                                                FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Philippines",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Philippines",c(6:ncol(secoal))],
                                                                    FUN = function(x){capfact["Philippines",cap_scenario] * x})
secoal[secoal$country == "Thailand",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Thailand",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Vietnam",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Vietnam",c(6:ncol(secoal))],
                                                                FUN = function(x){capfact["Vietnam",cap_scenario] * x})

all_proj <- data.frame(year = as.numeric(substr(projspan,2,5)), capacity = colSums(secoal[,6:ncol(secoal)]))
cambodia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Cambodia",6:ncol(secoal)]))
indonesia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                             capacity = colSums(secoal[secoal$country == "Indonesia",6:ncol(secoal)]))
laos_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                        capacity = colSums(secoal[secoal$country == "Laos",6:ncol(secoal)]))
malaysia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Malaysia",6:ncol(secoal)]))
myanmar_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                           capacity = colSums(secoal[secoal$country == "Myanmar",6:ncol(secoal)]))
philippines_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                               capacity = colSums(secoal[secoal$country == "Philippines",6:ncol(secoal)]))
thailand_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Thailand",6:ncol(secoal)]))
vietnam_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                           capacity = colSums(secoal[secoal$country == "Vietnam",6:ncol(secoal)]))

projectiondf <- data.frame(Year = c(firstyear,
                                    round((lastyear - firstyear)/4, 0) + firstyear,2*round((lastyear - firstyear)/4, 0) + firstyear,3*round((lastyear - firstyear)/4, 0) + firstyear,lastyear),
                           All = c(all_proj$capacity[all_proj$year == firstyear],
                                   all_proj$capacity[all_proj$year == round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == lastyear]),
                           Indonesia = c(indonesia_proj$capacity[indonesia_proj$year == firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == lastyear]),
                           Malaysia = c(malaysia_proj$capacity[malaysia_proj$year == firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == lastyear]),
                           Philippines = c(philippines_proj$capacity[philippines_proj$year == firstyear],
                                           philippines_proj$capacity[philippines_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == lastyear]),
                           Vietnam = c(vietnam_proj$capacity[vietnam_proj$year == firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == lastyear]))

projectiondf[,c(2:6)] <- round(projectiondf[,c(2:6)], 0)

countrylist <- c("All","Cambodia","Indonesia","Laos","Malaysia","Myanmar","Philippines","Thailand","Vietnam")
attrlist <- c(attr_All,attr_Cambodia,attr_Indonesia,attr_Laos,attr_Malaysia,attr_Myanmar,
              attr_Philippines,attr_Thailand,attr_Vietnam)
attrtab <- data.frame(Country = countrylist, Attrition = attrlist)
attrtab$Attrition <- round(attrtab$Attrition, 2)

co2f <- 8.2245*1000 * 215.225 * 0.00000000397347

all_proj$country <- "SE Asia"
cambodia_proj$country <- "Cambodia"
indonesia_proj$country <- "Indonesia"
laos_proj$country <- "Laos"
malaysia_proj$country <- "Malaysia"
myanmar_proj$country <- "Myanmar"
philippines_proj$country <- "Philippines"
thailand_proj$country <- "Thailand"
vietnam_proj$country <- "Vietnam"

bycountry <- rbind(all_proj, cambodia_proj, indonesia_proj, laos_proj, malaysia_proj, philippines_proj,
                   thailand_proj, vietnam_proj)

bycountry$label <- bycountry$country
bycountry$label[bycountry$year != max(bycountry$year)-round((max(bycountry$year)-min(bycountry$year))/1.3,0)] <- ""

bycountry_co2 <- bycountry
bycountry_co2$capacity <- bycountry_co2$capacity * co2f
colnames(bycountry_co2)[2] <- "co2_yearly"

bycountry_co2$co2_cumulative <- bycountry_co2$co2_yearly

for (i in 2:nrow(bycountry_co2)){
  bycountry_co2$co2_cumulative[i] <- ifelse(bycountry_co2$country[i-1] == bycountry_co2$country[i],
                                            bycountry_co2$co2_cumulative[i-1] + bycountry_co2$co2_cumulative[i],
                                            bycountry_co2$co2_cumulative[i])
}

#### PARAM. SET 3 EXPORTS ####

saveRDS(projectiondf, "projectiondf_p-by-p_3.rds")
saveRDS(bycountry_co2, "co2_p-by-p_3.rds")

#### PARAM. SET 4 (0.8 ATTRITION RATE) ####

lifespan <- 35 # operational lifespan for individual units

projspan <- 2017:2057 # year span for projections

attrition_specificity <- 1 # 0: region-wide attrition rate, 1: country-specific

attrition_scenario <- 0.8 # 1: middle attrition rate; 1<: high attrition rate; 1>: low attrition rate

cap_scenario <- "mid" # "low": low capacity factor; "mid": mid capacity factor; "high": high capacity factor

operating_year <- round(mean(secoal$commission_year, na.rm = T), 0) # est. commissioning year for operating units w/o a listed commissioning year

announced_time <- 10 # pipeline times
prepermit_time <- 7
permitted_time <- 4
construction_time <- 1

#### PARAM. SET 4 PROJECTIONS ####

projspan <- paste0("Y",projspan)
lastyear <- as.numeric(substr(last(projspan), 2, 5))
firstyear <- as.numeric(substr(first(projspan), 2, 5))

attr_All <- (status_percentages$All[3] + status_percentages$All[7])/
  (status_percentages$All[3] + status_percentages$All[7] + status_percentages$All[6])*attrition_scenario
attr_Cambodia <- (status_percentages$Cambodia[3] + status_percentages$Cambodia[7])/
  (status_percentages$Cambodia[3] + status_percentages$Cambodia[7] + status_percentages$Cambodia[6])*attrition_scenario
attr_Indonesia <- (status_percentages$Indonesia[3] + status_percentages$Indonesia[7])/
  (status_percentages$Indonesia[3] + status_percentages$Indonesia[7] + status_percentages$Indonesia[6])*attrition_scenario
attr_Laos <- (status_percentages$Laos[3] + status_percentages$Laos[7])/
  (status_percentages$Laos[3] + status_percentages$Laos[7] + status_percentages$Laos[6])*attrition_scenario
attr_Malaysia <- (status_percentages$Malaysia[3] + status_percentages$Malaysia[7])/
  (status_percentages$Malaysia[3] + status_percentages$Malaysia[7] + status_percentages$Malaysia[6])*attrition_scenario
attr_Myanmar <- (status_percentages$Myanmar[3] + status_percentages$Myanmar[7])/
  (status_percentages$Myanmar[3] + status_percentages$Myanmar[7] + status_percentages$Myanmar[6])*attrition_scenario
attr_Philippines <- (status_percentages$Philippines[3] + status_percentages$Philippines[7])/
  (status_percentages$Philippines[3] + status_percentages$Philippines[7] + status_percentages$Philippines[6])*attrition_scenario
attr_Thailand <- (status_percentages$Thailand[3] + status_percentages$Thailand[7])/
  (status_percentages$Thailand[3] + status_percentages$Thailand[7] + status_percentages$Thailand[6])*attrition_scenario
attr_Vietnam <- (status_percentages$Vietnam[3] + status_percentages$Vietnam[7])/
  (status_percentages$Vietnam[3] + status_percentages$Vietnam[7] + status_percentages$Vietnam[6])*attrition_scenario

attr_All <- ifelse(attr_All > 1, 1, attr_All)
attr_Cambodia <- ifelse(attr_Cambodia > 1, 1, attr_Cambodia)
attr_Indonesia <- ifelse(attr_Indonesia > 1, 1, attr_Indonesia)
attr_Laos <- ifelse(attr_Laos > 1, 1, attr_Laos)
attr_Malaysia <- ifelse(attr_Malaysia > 1, 1, attr_Malaysia)
attr_Myanmar <- ifelse(attr_Myanmar > 1, 1, attr_Myanmar)
attr_Philippines <- ifelse(attr_Philippines > 1, 1, attr_Philippines)
attr_Thailand <- ifelse(attr_Thailand > 1, 1, attr_Thailand)
attr_Vietnam <- ifelse(attr_Vietnam > 1, 1, attr_Vietnam)

if(attrition_specificity == 0){
  secoal$capacity[secoal$status != "operating" & !is.na(secoal$capacity)] <- secoal$capacity[
    secoal$status != "operating" & !is.na(secoal$capacity)]*(1-attr_All)
} else{
  for (i in 1:nrow(secoal)){
    secoal$capacity[i] <- ifelse(secoal$country[i] == "Cambodia" &
                                   secoal$status[i] != "operating",
                                 secoal$capacity[i]*(1-attr_Cambodia),
                                 ifelse(secoal$country[i] == "Indonesia" &
                                          secoal$status[i] != "operating",
                                        secoal$capacity[i]*(1-attr_Indonesia),
                                        ifelse(secoal$country[i] == "Laos" &
                                                 secoal$status[i] != "operating",
                                               secoal$capacity[i]*(1-attr_Laos),
                                               ifelse(secoal$country[i] == "Malaysia" &
                                                        secoal$status[i] != "operating",
                                                      secoal$capacity[i]*(1-attr_Malaysia),
                                                      ifelse(secoal$country[i] == "Myanmar" &
                                                               secoal$status[i] != "operating",
                                                             secoal$capacity[i]*(1-attr_Myanmar),
                                                             ifelse(secoal$country[i] == "Philippines" &
                                                                      secoal$status[i] != "operating",
                                                                    secoal$capacity[i]*(1-attr_Philippines),
                                                                    ifelse(secoal$country[i] == "Thailand" &
                                                                             secoal$status[i] != "operating",
                                                                           secoal$capacity[i]*(1-attr_Thailand),
                                                                           ifelse(secoal$country[i] == "Vietnam" &
                                                                                    secoal$status[i] != "operating",
                                                                                  secoal$capacity[i]*(1-attr_Vietnam),
                                                                                  secoal$capacity[i]))))))))
  }
}

secoal$commission_year[secoal$status == "announced"] <- 2017 + announced_time
secoal$commission_year[secoal$status == "pre-permit"] <- 2017 + prepermit_time
secoal$commission_year[secoal$status == "permitted"] <- 2017 + permitted_time
secoal$commission_year[secoal$status == "construction"] <- 2017 + construction_time
secoal$commission_year[secoal$status == "operating" & is.na(secoal$commission_year)] <- operating_year

secoal <- secoal[!is.na(secoal$commission_year),]

secoal$retirement_year <- as.numeric(as.character(secoal$commission_year)) + lifespan

secoal[projspan] <- NA

for (j in 6:ncol(secoal)){
  .year <- as.numeric(substr(colnames(secoal)[j],2,5))
  for (i in 1:nrow(secoal)){
    secoal[i,j] <- ifelse(.year >= secoal$commission_year[i] &
                            .year <= secoal$retirement_year[i],
                          1, 0)
  }
}

secoal[,c(6:ncol(secoal))] <- sapply(secoal[,c(6:ncol(secoal))],
                                     FUN = function(x){secoal$capacity * x})
secoal[secoal$country == "Cambodia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Cambodia",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Indonesia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Indonesia",c(6:ncol(secoal))],
                                                                  FUN = function(x){capfact["Indonesia",cap_scenario] * x})
secoal[secoal$country == "Laos",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Laos",c(6:ncol(secoal))],
                                                             FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Malaysia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Malaysia",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Malaysia",cap_scenario] * x})
secoal[secoal$country == "Myanmar",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Myanmar",c(6:ncol(secoal))],
                                                                FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Philippines",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Philippines",c(6:ncol(secoal))],
                                                                    FUN = function(x){capfact["Philippines",cap_scenario] * x})
secoal[secoal$country == "Thailand",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Thailand",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Vietnam",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Vietnam",c(6:ncol(secoal))],
                                                                FUN = function(x){capfact["Vietnam",cap_scenario] * x})

all_proj <- data.frame(year = as.numeric(substr(projspan,2,5)), capacity = colSums(secoal[,6:ncol(secoal)]))
cambodia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Cambodia",6:ncol(secoal)]))
indonesia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                             capacity = colSums(secoal[secoal$country == "Indonesia",6:ncol(secoal)]))
laos_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                        capacity = colSums(secoal[secoal$country == "Laos",6:ncol(secoal)]))
malaysia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Malaysia",6:ncol(secoal)]))
myanmar_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                           capacity = colSums(secoal[secoal$country == "Myanmar",6:ncol(secoal)]))
philippines_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                               capacity = colSums(secoal[secoal$country == "Philippines",6:ncol(secoal)]))
thailand_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Thailand",6:ncol(secoal)]))
vietnam_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                           capacity = colSums(secoal[secoal$country == "Vietnam",6:ncol(secoal)]))

projectiondf <- data.frame(Year = c(firstyear,
                                    round((lastyear - firstyear)/4, 0) + firstyear,2*round((lastyear - firstyear)/4, 0) + firstyear,3*round((lastyear - firstyear)/4, 0) + firstyear,lastyear),
                           All = c(all_proj$capacity[all_proj$year == firstyear],
                                   all_proj$capacity[all_proj$year == round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == lastyear]),
                           Indonesia = c(indonesia_proj$capacity[indonesia_proj$year == firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == lastyear]),
                           Malaysia = c(malaysia_proj$capacity[malaysia_proj$year == firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == lastyear]),
                           Philippines = c(philippines_proj$capacity[philippines_proj$year == firstyear],
                                           philippines_proj$capacity[philippines_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == lastyear]),
                           Vietnam = c(vietnam_proj$capacity[vietnam_proj$year == firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == lastyear]))

projectiondf[,c(2:6)] <- round(projectiondf[,c(2:6)], 0)

countrylist <- c("All","Cambodia","Indonesia","Laos","Malaysia","Myanmar","Philippines","Thailand","Vietnam")
attrlist <- c(attr_All,attr_Cambodia,attr_Indonesia,attr_Laos,attr_Malaysia,attr_Myanmar,
              attr_Philippines,attr_Thailand,attr_Vietnam)
attrtab <- data.frame(Country = countrylist, Attrition = attrlist)
attrtab$Attrition <- round(attrtab$Attrition, 2)

co2f <- 8.2245*1000 * 215.225 * 0.00000000397347

all_proj$country <- "SE Asia"
cambodia_proj$country <- "Cambodia"
indonesia_proj$country <- "Indonesia"
laos_proj$country <- "Laos"
malaysia_proj$country <- "Malaysia"
myanmar_proj$country <- "Myanmar"
philippines_proj$country <- "Philippines"
thailand_proj$country <- "Thailand"
vietnam_proj$country <- "Vietnam"

bycountry <- rbind(all_proj, cambodia_proj, indonesia_proj, laos_proj, malaysia_proj, philippines_proj,
                   thailand_proj, vietnam_proj)

bycountry$label <- bycountry$country
bycountry$label[bycountry$year != max(bycountry$year)-round((max(bycountry$year)-min(bycountry$year))/1.3,0)] <- ""

bycountry_co2 <- bycountry
bycountry_co2$capacity <- bycountry_co2$capacity * co2f
colnames(bycountry_co2)[2] <- "co2_yearly"

bycountry_co2$co2_cumulative <- bycountry_co2$co2_yearly

for (i in 2:nrow(bycountry_co2)){
  bycountry_co2$co2_cumulative[i] <- ifelse(bycountry_co2$country[i-1] == bycountry_co2$country[i],
                                            bycountry_co2$co2_cumulative[i-1] + bycountry_co2$co2_cumulative[i],
                                            bycountry_co2$co2_cumulative[i])
}

#### PARAM. SET 4 EXPORTS ####

saveRDS(projectiondf, "projectiondf_p-by-p_4.rds")
saveRDS(bycountry_co2, "co2_p-by-p_4.rds")

#### PARAM. SET 5 (1.2 ATTRITION RATE) ####

lifespan <- 35 # operational lifespan for individual units

projspan <- 2017:2057 # year span for projections

attrition_specificity <- 1 # 0: region-wide attrition rate, 1: country-specific

attrition_scenario <- 1.2 # 1: middle attrition rate; 1<: high attrition rate; 1>: low attrition rate

cap_scenario <- "mid" # "low": low capacity factor; "mid": mid capacity factor; "high": high capacity factor

operating_year <- round(mean(secoal$commission_year, na.rm = T), 0) # est. commissioning year for operating units w/o a listed commissioning year

announced_time <- 10 # pipeline times
prepermit_time <- 7
permitted_time <- 4
construction_time <- 1

#### PARAM. SET 5 PROJECTIONS ####

projspan <- paste0("Y",projspan)
lastyear <- as.numeric(substr(last(projspan), 2, 5))
firstyear <- as.numeric(substr(first(projspan), 2, 5))

attr_All <- (status_percentages$All[3] + status_percentages$All[7])/
  (status_percentages$All[3] + status_percentages$All[7] + status_percentages$All[6])*attrition_scenario
attr_Cambodia <- (status_percentages$Cambodia[3] + status_percentages$Cambodia[7])/
  (status_percentages$Cambodia[3] + status_percentages$Cambodia[7] + status_percentages$Cambodia[6])*attrition_scenario
attr_Indonesia <- (status_percentages$Indonesia[3] + status_percentages$Indonesia[7])/
  (status_percentages$Indonesia[3] + status_percentages$Indonesia[7] + status_percentages$Indonesia[6])*attrition_scenario
attr_Laos <- (status_percentages$Laos[3] + status_percentages$Laos[7])/
  (status_percentages$Laos[3] + status_percentages$Laos[7] + status_percentages$Laos[6])*attrition_scenario
attr_Malaysia <- (status_percentages$Malaysia[3] + status_percentages$Malaysia[7])/
  (status_percentages$Malaysia[3] + status_percentages$Malaysia[7] + status_percentages$Malaysia[6])*attrition_scenario
attr_Myanmar <- (status_percentages$Myanmar[3] + status_percentages$Myanmar[7])/
  (status_percentages$Myanmar[3] + status_percentages$Myanmar[7] + status_percentages$Myanmar[6])*attrition_scenario
attr_Philippines <- (status_percentages$Philippines[3] + status_percentages$Philippines[7])/
  (status_percentages$Philippines[3] + status_percentages$Philippines[7] + status_percentages$Philippines[6])*attrition_scenario
attr_Thailand <- (status_percentages$Thailand[3] + status_percentages$Thailand[7])/
  (status_percentages$Thailand[3] + status_percentages$Thailand[7] + status_percentages$Thailand[6])*attrition_scenario
attr_Vietnam <- (status_percentages$Vietnam[3] + status_percentages$Vietnam[7])/
  (status_percentages$Vietnam[3] + status_percentages$Vietnam[7] + status_percentages$Vietnam[6])*attrition_scenario

attr_All <- ifelse(attr_All > 1, 1, attr_All)
attr_Cambodia <- ifelse(attr_Cambodia > 1, 1, attr_Cambodia)
attr_Indonesia <- ifelse(attr_Indonesia > 1, 1, attr_Indonesia)
attr_Laos <- ifelse(attr_Laos > 1, 1, attr_Laos)
attr_Malaysia <- ifelse(attr_Malaysia > 1, 1, attr_Malaysia)
attr_Myanmar <- ifelse(attr_Myanmar > 1, 1, attr_Myanmar)
attr_Philippines <- ifelse(attr_Philippines > 1, 1, attr_Philippines)
attr_Thailand <- ifelse(attr_Thailand > 1, 1, attr_Thailand)
attr_Vietnam <- ifelse(attr_Vietnam > 1, 1, attr_Vietnam)

if(attrition_specificity == 0){
  secoal$capacity[secoal$status != "operating" & !is.na(secoal$capacity)] <- secoal$capacity[
    secoal$status != "operating" & !is.na(secoal$capacity)]*(1-attr_All)
} else{
  for (i in 1:nrow(secoal)){
    secoal$capacity[i] <- ifelse(secoal$country[i] == "Cambodia" &
                                   secoal$status[i] != "operating",
                                 secoal$capacity[i]*(1-attr_Cambodia),
                                 ifelse(secoal$country[i] == "Indonesia" &
                                          secoal$status[i] != "operating",
                                        secoal$capacity[i]*(1-attr_Indonesia),
                                        ifelse(secoal$country[i] == "Laos" &
                                                 secoal$status[i] != "operating",
                                               secoal$capacity[i]*(1-attr_Laos),
                                               ifelse(secoal$country[i] == "Malaysia" &
                                                        secoal$status[i] != "operating",
                                                      secoal$capacity[i]*(1-attr_Malaysia),
                                                      ifelse(secoal$country[i] == "Myanmar" &
                                                               secoal$status[i] != "operating",
                                                             secoal$capacity[i]*(1-attr_Myanmar),
                                                             ifelse(secoal$country[i] == "Philippines" &
                                                                      secoal$status[i] != "operating",
                                                                    secoal$capacity[i]*(1-attr_Philippines),
                                                                    ifelse(secoal$country[i] == "Thailand" &
                                                                             secoal$status[i] != "operating",
                                                                           secoal$capacity[i]*(1-attr_Thailand),
                                                                           ifelse(secoal$country[i] == "Vietnam" &
                                                                                    secoal$status[i] != "operating",
                                                                                  secoal$capacity[i]*(1-attr_Vietnam),
                                                                                  secoal$capacity[i]))))))))
  }
}

secoal$commission_year[secoal$status == "announced"] <- 2017 + announced_time
secoal$commission_year[secoal$status == "pre-permit"] <- 2017 + prepermit_time
secoal$commission_year[secoal$status == "permitted"] <- 2017 + permitted_time
secoal$commission_year[secoal$status == "construction"] <- 2017 + construction_time
secoal$commission_year[secoal$status == "operating" & is.na(secoal$commission_year)] <- operating_year

secoal <- secoal[!is.na(secoal$commission_year),]

secoal$retirement_year <- as.numeric(as.character(secoal$commission_year)) + lifespan

secoal[projspan] <- NA

for (j in 6:ncol(secoal)){
  .year <- as.numeric(substr(colnames(secoal)[j],2,5))
  for (i in 1:nrow(secoal)){
    secoal[i,j] <- ifelse(.year >= secoal$commission_year[i] &
                            .year <= secoal$retirement_year[i],
                          1, 0)
  }
}

secoal[,c(6:ncol(secoal))] <- sapply(secoal[,c(6:ncol(secoal))],
                                     FUN = function(x){secoal$capacity * x})
secoal[secoal$country == "Cambodia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Cambodia",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Indonesia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Indonesia",c(6:ncol(secoal))],
                                                                  FUN = function(x){capfact["Indonesia",cap_scenario] * x})
secoal[secoal$country == "Laos",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Laos",c(6:ncol(secoal))],
                                                             FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Malaysia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Malaysia",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Malaysia",cap_scenario] * x})
secoal[secoal$country == "Myanmar",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Myanmar",c(6:ncol(secoal))],
                                                                FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Philippines",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Philippines",c(6:ncol(secoal))],
                                                                    FUN = function(x){capfact["Philippines",cap_scenario] * x})
secoal[secoal$country == "Thailand",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Thailand",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Vietnam",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Vietnam",c(6:ncol(secoal))],
                                                                FUN = function(x){capfact["Vietnam",cap_scenario] * x})

all_proj <- data.frame(year = as.numeric(substr(projspan,2,5)), capacity = colSums(secoal[,6:ncol(secoal)]))
cambodia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Cambodia",6:ncol(secoal)]))
indonesia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                             capacity = colSums(secoal[secoal$country == "Indonesia",6:ncol(secoal)]))
laos_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                        capacity = colSums(secoal[secoal$country == "Laos",6:ncol(secoal)]))
malaysia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Malaysia",6:ncol(secoal)]))
myanmar_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                           capacity = colSums(secoal[secoal$country == "Myanmar",6:ncol(secoal)]))
philippines_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                               capacity = colSums(secoal[secoal$country == "Philippines",6:ncol(secoal)]))
thailand_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Thailand",6:ncol(secoal)]))
vietnam_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                           capacity = colSums(secoal[secoal$country == "Vietnam",6:ncol(secoal)]))

projectiondf <- data.frame(Year = c(firstyear,
                                    round((lastyear - firstyear)/4, 0) + firstyear,2*round((lastyear - firstyear)/4, 0) + firstyear,3*round((lastyear - firstyear)/4, 0) + firstyear,lastyear),
                           All = c(all_proj$capacity[all_proj$year == firstyear],
                                   all_proj$capacity[all_proj$year == round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == lastyear]),
                           Indonesia = c(indonesia_proj$capacity[indonesia_proj$year == firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == lastyear]),
                           Malaysia = c(malaysia_proj$capacity[malaysia_proj$year == firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == lastyear]),
                           Philippines = c(philippines_proj$capacity[philippines_proj$year == firstyear],
                                           philippines_proj$capacity[philippines_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == lastyear]),
                           Vietnam = c(vietnam_proj$capacity[vietnam_proj$year == firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == lastyear]))

projectiondf[,c(2:6)] <- round(projectiondf[,c(2:6)], 0)

countrylist <- c("All","Cambodia","Indonesia","Laos","Malaysia","Myanmar","Philippines","Thailand","Vietnam")
attrlist <- c(attr_All,attr_Cambodia,attr_Indonesia,attr_Laos,attr_Malaysia,attr_Myanmar,
              attr_Philippines,attr_Thailand,attr_Vietnam)
attrtab <- data.frame(Country = countrylist, Attrition = attrlist)
attrtab$Attrition <- round(attrtab$Attrition, 2)

co2f <- 8.2245*1000 * 215.225 * 0.00000000397347

all_proj$country <- "SE Asia"
cambodia_proj$country <- "Cambodia"
indonesia_proj$country <- "Indonesia"
laos_proj$country <- "Laos"
malaysia_proj$country <- "Malaysia"
myanmar_proj$country <- "Myanmar"
philippines_proj$country <- "Philippines"
thailand_proj$country <- "Thailand"
vietnam_proj$country <- "Vietnam"

bycountry <- rbind(all_proj, cambodia_proj, indonesia_proj, laos_proj, malaysia_proj, philippines_proj,
                   thailand_proj, vietnam_proj)

bycountry$label <- bycountry$country
bycountry$label[bycountry$year != max(bycountry$year)-round((max(bycountry$year)-min(bycountry$year))/1.3,0)] <- ""

bycountry_co2 <- bycountry
bycountry_co2$capacity <- bycountry_co2$capacity * co2f
colnames(bycountry_co2)[2] <- "co2_yearly"

bycountry_co2$co2_cumulative <- bycountry_co2$co2_yearly

for (i in 2:nrow(bycountry_co2)){
  bycountry_co2$co2_cumulative[i] <- ifelse(bycountry_co2$country[i-1] == bycountry_co2$country[i],
                                            bycountry_co2$co2_cumulative[i-1] + bycountry_co2$co2_cumulative[i],
                                            bycountry_co2$co2_cumulative[i])
}

#### PARAM. SET 5 EXPORTS ####

saveRDS(projectiondf, "projectiondf_p-by-p_5.rds")
saveRDS(bycountry_co2, "co2_p-by-p_5.rds")

#### PARAM. SET 6 (ONE YEAR LONGER PIPELINE TIMES) ####

lifespan <- 35 # operational lifespan for individual units

projspan <- 2017:2057 # year span for projections

attrition_specificity <- 1 # 0: region-wide attrition rate, 1: country-specific

attrition_scenario <- 1 # 1: middle attrition rate; 1<: high attrition rate; 1>: low attrition rate

cap_scenario <- "mid" # "low": low capacity factor; "mid": mid capacity factor; "high": high capacity factor

operating_year <- round(mean(secoal$commission_year, na.rm = T), 0) # est. commissioning year for operating units w/o a listed commissioning year

announced_time <- 11 # pipeline times
prepermit_time <- 8
permitted_time <- 5
construction_time <- 2

#### PARAM. SET 6 PROJECTIONS ####

projspan <- paste0("Y",projspan)
lastyear <- as.numeric(substr(last(projspan), 2, 5))
firstyear <- as.numeric(substr(first(projspan), 2, 5))

attr_All <- (status_percentages$All[3] + status_percentages$All[7])/
  (status_percentages$All[3] + status_percentages$All[7] + status_percentages$All[6])*attrition_scenario
attr_Cambodia <- (status_percentages$Cambodia[3] + status_percentages$Cambodia[7])/
  (status_percentages$Cambodia[3] + status_percentages$Cambodia[7] + status_percentages$Cambodia[6])*attrition_scenario
attr_Indonesia <- (status_percentages$Indonesia[3] + status_percentages$Indonesia[7])/
  (status_percentages$Indonesia[3] + status_percentages$Indonesia[7] + status_percentages$Indonesia[6])*attrition_scenario
attr_Laos <- (status_percentages$Laos[3] + status_percentages$Laos[7])/
  (status_percentages$Laos[3] + status_percentages$Laos[7] + status_percentages$Laos[6])*attrition_scenario
attr_Malaysia <- (status_percentages$Malaysia[3] + status_percentages$Malaysia[7])/
  (status_percentages$Malaysia[3] + status_percentages$Malaysia[7] + status_percentages$Malaysia[6])*attrition_scenario
attr_Myanmar <- (status_percentages$Myanmar[3] + status_percentages$Myanmar[7])/
  (status_percentages$Myanmar[3] + status_percentages$Myanmar[7] + status_percentages$Myanmar[6])*attrition_scenario
attr_Philippines <- (status_percentages$Philippines[3] + status_percentages$Philippines[7])/
  (status_percentages$Philippines[3] + status_percentages$Philippines[7] + status_percentages$Philippines[6])*attrition_scenario
attr_Thailand <- (status_percentages$Thailand[3] + status_percentages$Thailand[7])/
  (status_percentages$Thailand[3] + status_percentages$Thailand[7] + status_percentages$Thailand[6])*attrition_scenario
attr_Vietnam <- (status_percentages$Vietnam[3] + status_percentages$Vietnam[7])/
  (status_percentages$Vietnam[3] + status_percentages$Vietnam[7] + status_percentages$Vietnam[6])*attrition_scenario

attr_All <- ifelse(attr_All > 1, 1, attr_All)
attr_Cambodia <- ifelse(attr_Cambodia > 1, 1, attr_Cambodia)
attr_Indonesia <- ifelse(attr_Indonesia > 1, 1, attr_Indonesia)
attr_Laos <- ifelse(attr_Laos > 1, 1, attr_Laos)
attr_Malaysia <- ifelse(attr_Malaysia > 1, 1, attr_Malaysia)
attr_Myanmar <- ifelse(attr_Myanmar > 1, 1, attr_Myanmar)
attr_Philippines <- ifelse(attr_Philippines > 1, 1, attr_Philippines)
attr_Thailand <- ifelse(attr_Thailand > 1, 1, attr_Thailand)
attr_Vietnam <- ifelse(attr_Vietnam > 1, 1, attr_Vietnam)

if(attrition_specificity == 0){
  secoal$capacity[secoal$status != "operating" & !is.na(secoal$capacity)] <- secoal$capacity[
    secoal$status != "operating" & !is.na(secoal$capacity)]*(1-attr_All)
} else{
  for (i in 1:nrow(secoal)){
    secoal$capacity[i] <- ifelse(secoal$country[i] == "Cambodia" &
                                   secoal$status[i] != "operating",
                                 secoal$capacity[i]*(1-attr_Cambodia),
                                 ifelse(secoal$country[i] == "Indonesia" &
                                          secoal$status[i] != "operating",
                                        secoal$capacity[i]*(1-attr_Indonesia),
                                        ifelse(secoal$country[i] == "Laos" &
                                                 secoal$status[i] != "operating",
                                               secoal$capacity[i]*(1-attr_Laos),
                                               ifelse(secoal$country[i] == "Malaysia" &
                                                        secoal$status[i] != "operating",
                                                      secoal$capacity[i]*(1-attr_Malaysia),
                                                      ifelse(secoal$country[i] == "Myanmar" &
                                                               secoal$status[i] != "operating",
                                                             secoal$capacity[i]*(1-attr_Myanmar),
                                                             ifelse(secoal$country[i] == "Philippines" &
                                                                      secoal$status[i] != "operating",
                                                                    secoal$capacity[i]*(1-attr_Philippines),
                                                                    ifelse(secoal$country[i] == "Thailand" &
                                                                             secoal$status[i] != "operating",
                                                                           secoal$capacity[i]*(1-attr_Thailand),
                                                                           ifelse(secoal$country[i] == "Vietnam" &
                                                                                    secoal$status[i] != "operating",
                                                                                  secoal$capacity[i]*(1-attr_Vietnam),
                                                                                  secoal$capacity[i]))))))))
  }
}

secoal$commission_year[secoal$status == "announced"] <- 2017 + announced_time
secoal$commission_year[secoal$status == "pre-permit"] <- 2017 + prepermit_time
secoal$commission_year[secoal$status == "permitted"] <- 2017 + permitted_time
secoal$commission_year[secoal$status == "construction"] <- 2017 + construction_time
secoal$commission_year[secoal$status == "operating" & is.na(secoal$commission_year)] <- operating_year

secoal <- secoal[!is.na(secoal$commission_year),]

secoal$retirement_year <- as.numeric(as.character(secoal$commission_year)) + lifespan

secoal[projspan] <- NA

for (j in 6:ncol(secoal)){
  .year <- as.numeric(substr(colnames(secoal)[j],2,5))
  for (i in 1:nrow(secoal)){
    secoal[i,j] <- ifelse(.year >= secoal$commission_year[i] &
                            .year <= secoal$retirement_year[i],
                          1, 0)
  }
}

secoal[,c(6:ncol(secoal))] <- sapply(secoal[,c(6:ncol(secoal))],
                                     FUN = function(x){secoal$capacity * x})
secoal[secoal$country == "Cambodia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Cambodia",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Indonesia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Indonesia",c(6:ncol(secoal))],
                                                                  FUN = function(x){capfact["Indonesia",cap_scenario] * x})
secoal[secoal$country == "Laos",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Laos",c(6:ncol(secoal))],
                                                             FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Malaysia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Malaysia",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Malaysia",cap_scenario] * x})
secoal[secoal$country == "Myanmar",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Myanmar",c(6:ncol(secoal))],
                                                                FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Philippines",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Philippines",c(6:ncol(secoal))],
                                                                    FUN = function(x){capfact["Philippines",cap_scenario] * x})
secoal[secoal$country == "Thailand",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Thailand",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Vietnam",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Vietnam",c(6:ncol(secoal))],
                                                                FUN = function(x){capfact["Vietnam",cap_scenario] * x})

all_proj <- data.frame(year = as.numeric(substr(projspan,2,5)), capacity = colSums(secoal[,6:ncol(secoal)]))
cambodia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Cambodia",6:ncol(secoal)]))
indonesia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                             capacity = colSums(secoal[secoal$country == "Indonesia",6:ncol(secoal)]))
laos_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                        capacity = colSums(secoal[secoal$country == "Laos",6:ncol(secoal)]))
malaysia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Malaysia",6:ncol(secoal)]))
myanmar_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                           capacity = colSums(secoal[secoal$country == "Myanmar",6:ncol(secoal)]))
philippines_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                               capacity = colSums(secoal[secoal$country == "Philippines",6:ncol(secoal)]))
thailand_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Thailand",6:ncol(secoal)]))
vietnam_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                           capacity = colSums(secoal[secoal$country == "Vietnam",6:ncol(secoal)]))

projectiondf <- data.frame(Year = c(firstyear,
                                    round((lastyear - firstyear)/4, 0) + firstyear,2*round((lastyear - firstyear)/4, 0) + firstyear,3*round((lastyear - firstyear)/4, 0) + firstyear,lastyear),
                           All = c(all_proj$capacity[all_proj$year == firstyear],
                                   all_proj$capacity[all_proj$year == round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == lastyear]),
                           Indonesia = c(indonesia_proj$capacity[indonesia_proj$year == firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == lastyear]),
                           Malaysia = c(malaysia_proj$capacity[malaysia_proj$year == firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == lastyear]),
                           Philippines = c(philippines_proj$capacity[philippines_proj$year == firstyear],
                                           philippines_proj$capacity[philippines_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == lastyear]),
                           Vietnam = c(vietnam_proj$capacity[vietnam_proj$year == firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == lastyear]))

projectiondf[,c(2:6)] <- round(projectiondf[,c(2:6)], 0)

countrylist <- c("All","Cambodia","Indonesia","Laos","Malaysia","Myanmar","Philippines","Thailand","Vietnam")
attrlist <- c(attr_All,attr_Cambodia,attr_Indonesia,attr_Laos,attr_Malaysia,attr_Myanmar,
              attr_Philippines,attr_Thailand,attr_Vietnam)
attrtab <- data.frame(Country = countrylist, Attrition = attrlist)
attrtab$Attrition <- round(attrtab$Attrition, 2)

co2f <- 8.2245*1000 * 215.225 * 0.00000000397347

all_proj$country <- "SE Asia"
cambodia_proj$country <- "Cambodia"
indonesia_proj$country <- "Indonesia"
laos_proj$country <- "Laos"
malaysia_proj$country <- "Malaysia"
myanmar_proj$country <- "Myanmar"
philippines_proj$country <- "Philippines"
thailand_proj$country <- "Thailand"
vietnam_proj$country <- "Vietnam"

bycountry <- rbind(all_proj, cambodia_proj, indonesia_proj, laos_proj, malaysia_proj, philippines_proj,
                   thailand_proj, vietnam_proj)

bycountry$label <- bycountry$country
bycountry$label[bycountry$year != max(bycountry$year)-round((max(bycountry$year)-min(bycountry$year))/1.3,0)] <- ""

bycountry_co2 <- bycountry
bycountry_co2$capacity <- bycountry_co2$capacity * co2f
colnames(bycountry_co2)[2] <- "co2_yearly"

bycountry_co2$co2_cumulative <- bycountry_co2$co2_yearly

for (i in 2:nrow(bycountry_co2)){
  bycountry_co2$co2_cumulative[i] <- ifelse(bycountry_co2$country[i-1] == bycountry_co2$country[i],
                                            bycountry_co2$co2_cumulative[i-1] + bycountry_co2$co2_cumulative[i],
                                            bycountry_co2$co2_cumulative[i])
}

#### PARAM. SET 6 EXPORTS ####

saveRDS(projectiondf, "projectiondf_p-by-p_6.rds")
saveRDS(bycountry_co2, "co2_p-by-p_6.rds")

#### PARAM. SET 7 (ONE YEAR SHORTER PIPELINE TIMES) ####

lifespan <- 35 # operational lifespan for individual units

projspan <- 2017:2057 # year span for projections

attrition_specificity <- 1 # 0: region-wide attrition rate, 1: country-specific

attrition_scenario <- 1 # 1: middle attrition rate; 1<: high attrition rate; 1>: low attrition rate

cap_scenario <- "mid" # "low": low capacity factor; "mid": mid capacity factor; "high": high capacity factor

operating_year <- round(mean(secoal$commission_year, na.rm = T), 0) # est. commissioning year for operating units w/o a listed commissioning year

announced_time <- 9 # pipeline times
prepermit_time <- 6
permitted_time <- 3
construction_time <- 0

#### PARAM. SET 7 PROJECTIONS ####

projspan <- paste0("Y",projspan)
lastyear <- as.numeric(substr(last(projspan), 2, 5))
firstyear <- as.numeric(substr(first(projspan), 2, 5))

attr_All <- (status_percentages$All[3] + status_percentages$All[7])/
  (status_percentages$All[3] + status_percentages$All[7] + status_percentages$All[6])*attrition_scenario
attr_Cambodia <- (status_percentages$Cambodia[3] + status_percentages$Cambodia[7])/
  (status_percentages$Cambodia[3] + status_percentages$Cambodia[7] + status_percentages$Cambodia[6])*attrition_scenario
attr_Indonesia <- (status_percentages$Indonesia[3] + status_percentages$Indonesia[7])/
  (status_percentages$Indonesia[3] + status_percentages$Indonesia[7] + status_percentages$Indonesia[6])*attrition_scenario
attr_Laos <- (status_percentages$Laos[3] + status_percentages$Laos[7])/
  (status_percentages$Laos[3] + status_percentages$Laos[7] + status_percentages$Laos[6])*attrition_scenario
attr_Malaysia <- (status_percentages$Malaysia[3] + status_percentages$Malaysia[7])/
  (status_percentages$Malaysia[3] + status_percentages$Malaysia[7] + status_percentages$Malaysia[6])*attrition_scenario
attr_Myanmar <- (status_percentages$Myanmar[3] + status_percentages$Myanmar[7])/
  (status_percentages$Myanmar[3] + status_percentages$Myanmar[7] + status_percentages$Myanmar[6])*attrition_scenario
attr_Philippines <- (status_percentages$Philippines[3] + status_percentages$Philippines[7])/
  (status_percentages$Philippines[3] + status_percentages$Philippines[7] + status_percentages$Philippines[6])*attrition_scenario
attr_Thailand <- (status_percentages$Thailand[3] + status_percentages$Thailand[7])/
  (status_percentages$Thailand[3] + status_percentages$Thailand[7] + status_percentages$Thailand[6])*attrition_scenario
attr_Vietnam <- (status_percentages$Vietnam[3] + status_percentages$Vietnam[7])/
  (status_percentages$Vietnam[3] + status_percentages$Vietnam[7] + status_percentages$Vietnam[6])*attrition_scenario

attr_All <- ifelse(attr_All > 1, 1, attr_All)
attr_Cambodia <- ifelse(attr_Cambodia > 1, 1, attr_Cambodia)
attr_Indonesia <- ifelse(attr_Indonesia > 1, 1, attr_Indonesia)
attr_Laos <- ifelse(attr_Laos > 1, 1, attr_Laos)
attr_Malaysia <- ifelse(attr_Malaysia > 1, 1, attr_Malaysia)
attr_Myanmar <- ifelse(attr_Myanmar > 1, 1, attr_Myanmar)
attr_Philippines <- ifelse(attr_Philippines > 1, 1, attr_Philippines)
attr_Thailand <- ifelse(attr_Thailand > 1, 1, attr_Thailand)
attr_Vietnam <- ifelse(attr_Vietnam > 1, 1, attr_Vietnam)

if(attrition_specificity == 0){
  secoal$capacity[secoal$status != "operating" & !is.na(secoal$capacity)] <- secoal$capacity[
    secoal$status != "operating" & !is.na(secoal$capacity)]*(1-attr_All)
} else{
  for (i in 1:nrow(secoal)){
    secoal$capacity[i] <- ifelse(secoal$country[i] == "Cambodia" &
                                   secoal$status[i] != "operating",
                                 secoal$capacity[i]*(1-attr_Cambodia),
                                 ifelse(secoal$country[i] == "Indonesia" &
                                          secoal$status[i] != "operating",
                                        secoal$capacity[i]*(1-attr_Indonesia),
                                        ifelse(secoal$country[i] == "Laos" &
                                                 secoal$status[i] != "operating",
                                               secoal$capacity[i]*(1-attr_Laos),
                                               ifelse(secoal$country[i] == "Malaysia" &
                                                        secoal$status[i] != "operating",
                                                      secoal$capacity[i]*(1-attr_Malaysia),
                                                      ifelse(secoal$country[i] == "Myanmar" &
                                                               secoal$status[i] != "operating",
                                                             secoal$capacity[i]*(1-attr_Myanmar),
                                                             ifelse(secoal$country[i] == "Philippines" &
                                                                      secoal$status[i] != "operating",
                                                                    secoal$capacity[i]*(1-attr_Philippines),
                                                                    ifelse(secoal$country[i] == "Thailand" &
                                                                             secoal$status[i] != "operating",
                                                                           secoal$capacity[i]*(1-attr_Thailand),
                                                                           ifelse(secoal$country[i] == "Vietnam" &
                                                                                    secoal$status[i] != "operating",
                                                                                  secoal$capacity[i]*(1-attr_Vietnam),
                                                                                  secoal$capacity[i]))))))))
  }
}

secoal$commission_year[secoal$status == "announced"] <- 2017 + announced_time
secoal$commission_year[secoal$status == "pre-permit"] <- 2017 + prepermit_time
secoal$commission_year[secoal$status == "permitted"] <- 2017 + permitted_time
secoal$commission_year[secoal$status == "construction"] <- 2017 + construction_time
secoal$commission_year[secoal$status == "operating" & is.na(secoal$commission_year)] <- operating_year

secoal <- secoal[!is.na(secoal$commission_year),]

secoal$retirement_year <- as.numeric(as.character(secoal$commission_year)) + lifespan

secoal[projspan] <- NA

for (j in 6:ncol(secoal)){
  .year <- as.numeric(substr(colnames(secoal)[j],2,5))
  for (i in 1:nrow(secoal)){
    secoal[i,j] <- ifelse(.year >= secoal$commission_year[i] &
                            .year <= secoal$retirement_year[i],
                          1, 0)
  }
}

secoal[,c(6:ncol(secoal))] <- sapply(secoal[,c(6:ncol(secoal))],
                                     FUN = function(x){secoal$capacity * x})
secoal[secoal$country == "Cambodia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Cambodia",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Indonesia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Indonesia",c(6:ncol(secoal))],
                                                                  FUN = function(x){capfact["Indonesia",cap_scenario] * x})
secoal[secoal$country == "Laos",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Laos",c(6:ncol(secoal))],
                                                             FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Malaysia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Malaysia",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Malaysia",cap_scenario] * x})
secoal[secoal$country == "Myanmar",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Myanmar",c(6:ncol(secoal))],
                                                                FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Philippines",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Philippines",c(6:ncol(secoal))],
                                                                    FUN = function(x){capfact["Philippines",cap_scenario] * x})
secoal[secoal$country == "Thailand",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Thailand",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Vietnam",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Vietnam",c(6:ncol(secoal))],
                                                                FUN = function(x){capfact["Vietnam",cap_scenario] * x})

all_proj <- data.frame(year = as.numeric(substr(projspan,2,5)), capacity = colSums(secoal[,6:ncol(secoal)]))
cambodia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Cambodia",6:ncol(secoal)]))
indonesia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                             capacity = colSums(secoal[secoal$country == "Indonesia",6:ncol(secoal)]))
laos_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                        capacity = colSums(secoal[secoal$country == "Laos",6:ncol(secoal)]))
malaysia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Malaysia",6:ncol(secoal)]))
myanmar_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                           capacity = colSums(secoal[secoal$country == "Myanmar",6:ncol(secoal)]))
philippines_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                               capacity = colSums(secoal[secoal$country == "Philippines",6:ncol(secoal)]))
thailand_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Thailand",6:ncol(secoal)]))
vietnam_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                           capacity = colSums(secoal[secoal$country == "Vietnam",6:ncol(secoal)]))

projectiondf <- data.frame(Year = c(firstyear,
                                    round((lastyear - firstyear)/4, 0) + firstyear,2*round((lastyear - firstyear)/4, 0) + firstyear,3*round((lastyear - firstyear)/4, 0) + firstyear,lastyear),
                           All = c(all_proj$capacity[all_proj$year == firstyear],
                                   all_proj$capacity[all_proj$year == round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == lastyear]),
                           Indonesia = c(indonesia_proj$capacity[indonesia_proj$year == firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == lastyear]),
                           Malaysia = c(malaysia_proj$capacity[malaysia_proj$year == firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == lastyear]),
                           Philippines = c(philippines_proj$capacity[philippines_proj$year == firstyear],
                                           philippines_proj$capacity[philippines_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == lastyear]),
                           Vietnam = c(vietnam_proj$capacity[vietnam_proj$year == firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == lastyear]))

projectiondf[,c(2:6)] <- round(projectiondf[,c(2:6)], 0)

countrylist <- c("All","Cambodia","Indonesia","Laos","Malaysia","Myanmar","Philippines","Thailand","Vietnam")
attrlist <- c(attr_All,attr_Cambodia,attr_Indonesia,attr_Laos,attr_Malaysia,attr_Myanmar,
              attr_Philippines,attr_Thailand,attr_Vietnam)
attrtab <- data.frame(Country = countrylist, Attrition = attrlist)
attrtab$Attrition <- round(attrtab$Attrition, 2)

co2f <- 8.2245*1000 * 215.225 * 0.00000000397347

all_proj$country <- "SE Asia"
cambodia_proj$country <- "Cambodia"
indonesia_proj$country <- "Indonesia"
laos_proj$country <- "Laos"
malaysia_proj$country <- "Malaysia"
myanmar_proj$country <- "Myanmar"
philippines_proj$country <- "Philippines"
thailand_proj$country <- "Thailand"
vietnam_proj$country <- "Vietnam"

bycountry <- rbind(all_proj, cambodia_proj, indonesia_proj, laos_proj, malaysia_proj, philippines_proj,
                   thailand_proj, vietnam_proj)

bycountry$label <- bycountry$country
bycountry$label[bycountry$year != max(bycountry$year)-round((max(bycountry$year)-min(bycountry$year))/1.3,0)] <- ""

bycountry_co2 <- bycountry
bycountry_co2$capacity <- bycountry_co2$capacity * co2f
colnames(bycountry_co2)[2] <- "co2_yearly"

bycountry_co2$co2_cumulative <- bycountry_co2$co2_yearly

for (i in 2:nrow(bycountry_co2)){
  bycountry_co2$co2_cumulative[i] <- ifelse(bycountry_co2$country[i-1] == bycountry_co2$country[i],
                                            bycountry_co2$co2_cumulative[i-1] + bycountry_co2$co2_cumulative[i],
                                            bycountry_co2$co2_cumulative[i])
}

#### PARAM. SET 7 EXPORTS ####

saveRDS(projectiondf, "projectiondf_p-by-p_7.rds")
saveRDS(bycountry_co2, "co2_p-by-p_7.rds")

#### PARAM. SET 8 (LOW LIFESPAN) ####

lifespan <- 30 # operational lifespan for individual units

projspan <- 2017:2057 # year span for projections

attrition_specificity <- 1 # 0: region-wide attrition rate, 1: country-specific

attrition_scenario <- 1 # 1: middle attrition rate; 1<: high attrition rate; 1>: low attrition rate

cap_scenario <- "mid" # "low": low capacity factor; "mid": mid capacity factor; "high": high capacity factor

operating_year <- round(mean(secoal$commission_year, na.rm = T), 0) # est. commissioning year for operating units w/o a listed commissioning year

announced_time <- 10 # pipeline times
prepermit_time <- 7
permitted_time <- 4
construction_time <- 1

#### PARAM. SET 8 PROJECTIONS ####

projspan <- paste0("Y",projspan)
lastyear <- as.numeric(substr(last(projspan), 2, 5))
firstyear <- as.numeric(substr(first(projspan), 2, 5))

attr_All <- (status_percentages$All[3] + status_percentages$All[7])/
  (status_percentages$All[3] + status_percentages$All[7] + status_percentages$All[6])*attrition_scenario
attr_Cambodia <- (status_percentages$Cambodia[3] + status_percentages$Cambodia[7])/
  (status_percentages$Cambodia[3] + status_percentages$Cambodia[7] + status_percentages$Cambodia[6])*attrition_scenario
attr_Indonesia <- (status_percentages$Indonesia[3] + status_percentages$Indonesia[7])/
  (status_percentages$Indonesia[3] + status_percentages$Indonesia[7] + status_percentages$Indonesia[6])*attrition_scenario
attr_Laos <- (status_percentages$Laos[3] + status_percentages$Laos[7])/
  (status_percentages$Laos[3] + status_percentages$Laos[7] + status_percentages$Laos[6])*attrition_scenario
attr_Malaysia <- (status_percentages$Malaysia[3] + status_percentages$Malaysia[7])/
  (status_percentages$Malaysia[3] + status_percentages$Malaysia[7] + status_percentages$Malaysia[6])*attrition_scenario
attr_Myanmar <- (status_percentages$Myanmar[3] + status_percentages$Myanmar[7])/
  (status_percentages$Myanmar[3] + status_percentages$Myanmar[7] + status_percentages$Myanmar[6])*attrition_scenario
attr_Philippines <- (status_percentages$Philippines[3] + status_percentages$Philippines[7])/
  (status_percentages$Philippines[3] + status_percentages$Philippines[7] + status_percentages$Philippines[6])*attrition_scenario
attr_Thailand <- (status_percentages$Thailand[3] + status_percentages$Thailand[7])/
  (status_percentages$Thailand[3] + status_percentages$Thailand[7] + status_percentages$Thailand[6])*attrition_scenario
attr_Vietnam <- (status_percentages$Vietnam[3] + status_percentages$Vietnam[7])/
  (status_percentages$Vietnam[3] + status_percentages$Vietnam[7] + status_percentages$Vietnam[6])*attrition_scenario

attr_All <- ifelse(attr_All > 1, 1, attr_All)
attr_Cambodia <- ifelse(attr_Cambodia > 1, 1, attr_Cambodia)
attr_Indonesia <- ifelse(attr_Indonesia > 1, 1, attr_Indonesia)
attr_Laos <- ifelse(attr_Laos > 1, 1, attr_Laos)
attr_Malaysia <- ifelse(attr_Malaysia > 1, 1, attr_Malaysia)
attr_Myanmar <- ifelse(attr_Myanmar > 1, 1, attr_Myanmar)
attr_Philippines <- ifelse(attr_Philippines > 1, 1, attr_Philippines)
attr_Thailand <- ifelse(attr_Thailand > 1, 1, attr_Thailand)
attr_Vietnam <- ifelse(attr_Vietnam > 1, 1, attr_Vietnam)

if(attrition_specificity == 0){
  secoal$capacity[secoal$status != "operating" & !is.na(secoal$capacity)] <- secoal$capacity[
    secoal$status != "operating" & !is.na(secoal$capacity)]*(1-attr_All)
} else{
  for (i in 1:nrow(secoal)){
    secoal$capacity[i] <- ifelse(secoal$country[i] == "Cambodia" &
                                   secoal$status[i] != "operating",
                                 secoal$capacity[i]*(1-attr_Cambodia),
                                 ifelse(secoal$country[i] == "Indonesia" &
                                          secoal$status[i] != "operating",
                                        secoal$capacity[i]*(1-attr_Indonesia),
                                        ifelse(secoal$country[i] == "Laos" &
                                                 secoal$status[i] != "operating",
                                               secoal$capacity[i]*(1-attr_Laos),
                                               ifelse(secoal$country[i] == "Malaysia" &
                                                        secoal$status[i] != "operating",
                                                      secoal$capacity[i]*(1-attr_Malaysia),
                                                      ifelse(secoal$country[i] == "Myanmar" &
                                                               secoal$status[i] != "operating",
                                                             secoal$capacity[i]*(1-attr_Myanmar),
                                                             ifelse(secoal$country[i] == "Philippines" &
                                                                      secoal$status[i] != "operating",
                                                                    secoal$capacity[i]*(1-attr_Philippines),
                                                                    ifelse(secoal$country[i] == "Thailand" &
                                                                             secoal$status[i] != "operating",
                                                                           secoal$capacity[i]*(1-attr_Thailand),
                                                                           ifelse(secoal$country[i] == "Vietnam" &
                                                                                    secoal$status[i] != "operating",
                                                                                  secoal$capacity[i]*(1-attr_Vietnam),
                                                                                  secoal$capacity[i]))))))))
  }
}

secoal$commission_year[secoal$status == "announced"] <- 2017 + announced_time
secoal$commission_year[secoal$status == "pre-permit"] <- 2017 + prepermit_time
secoal$commission_year[secoal$status == "permitted"] <- 2017 + permitted_time
secoal$commission_year[secoal$status == "construction"] <- 2017 + construction_time
secoal$commission_year[secoal$status == "operating" & is.na(secoal$commission_year)] <- operating_year

secoal <- secoal[!is.na(secoal$commission_year),]

secoal$retirement_year <- as.numeric(as.character(secoal$commission_year)) + lifespan

secoal[projspan] <- NA

for (j in 6:ncol(secoal)){
  .year <- as.numeric(substr(colnames(secoal)[j],2,5))
  for (i in 1:nrow(secoal)){
    secoal[i,j] <- ifelse(.year >= secoal$commission_year[i] &
                            .year <= secoal$retirement_year[i],
                          1, 0)
  }
}

secoal[,c(6:ncol(secoal))] <- sapply(secoal[,c(6:ncol(secoal))],
                                     FUN = function(x){secoal$capacity * x})
secoal[secoal$country == "Cambodia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Cambodia",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Indonesia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Indonesia",c(6:ncol(secoal))],
                                                                  FUN = function(x){capfact["Indonesia",cap_scenario] * x})
secoal[secoal$country == "Laos",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Laos",c(6:ncol(secoal))],
                                                             FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Malaysia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Malaysia",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Malaysia",cap_scenario] * x})
secoal[secoal$country == "Myanmar",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Myanmar",c(6:ncol(secoal))],
                                                                FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Philippines",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Philippines",c(6:ncol(secoal))],
                                                                    FUN = function(x){capfact["Philippines",cap_scenario] * x})
secoal[secoal$country == "Thailand",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Thailand",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Vietnam",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Vietnam",c(6:ncol(secoal))],
                                                                FUN = function(x){capfact["Vietnam",cap_scenario] * x})

all_proj <- data.frame(year = as.numeric(substr(projspan,2,5)), capacity = colSums(secoal[,6:ncol(secoal)]))
cambodia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Cambodia",6:ncol(secoal)]))
indonesia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                             capacity = colSums(secoal[secoal$country == "Indonesia",6:ncol(secoal)]))
laos_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                        capacity = colSums(secoal[secoal$country == "Laos",6:ncol(secoal)]))
malaysia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Malaysia",6:ncol(secoal)]))
myanmar_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                           capacity = colSums(secoal[secoal$country == "Myanmar",6:ncol(secoal)]))
philippines_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                               capacity = colSums(secoal[secoal$country == "Philippines",6:ncol(secoal)]))
thailand_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Thailand",6:ncol(secoal)]))
vietnam_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                           capacity = colSums(secoal[secoal$country == "Vietnam",6:ncol(secoal)]))

projectiondf <- data.frame(Year = c(firstyear,
                                    round((lastyear - firstyear)/4, 0) + firstyear,2*round((lastyear - firstyear)/4, 0) + firstyear,3*round((lastyear - firstyear)/4, 0) + firstyear,lastyear),
                           All = c(all_proj$capacity[all_proj$year == firstyear],
                                   all_proj$capacity[all_proj$year == round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == lastyear]),
                           Indonesia = c(indonesia_proj$capacity[indonesia_proj$year == firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == lastyear]),
                           Malaysia = c(malaysia_proj$capacity[malaysia_proj$year == firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == lastyear]),
                           Philippines = c(philippines_proj$capacity[philippines_proj$year == firstyear],
                                           philippines_proj$capacity[philippines_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == lastyear]),
                           Vietnam = c(vietnam_proj$capacity[vietnam_proj$year == firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == lastyear]))

projectiondf[,c(2:6)] <- round(projectiondf[,c(2:6)], 0)

countrylist <- c("All","Cambodia","Indonesia","Laos","Malaysia","Myanmar","Philippines","Thailand","Vietnam")
attrlist <- c(attr_All,attr_Cambodia,attr_Indonesia,attr_Laos,attr_Malaysia,attr_Myanmar,
              attr_Philippines,attr_Thailand,attr_Vietnam)
attrtab <- data.frame(Country = countrylist, Attrition = attrlist)
attrtab$Attrition <- round(attrtab$Attrition, 2)

co2f <- 8.2245*1000 * 215.225 * 0.00000000397347

all_proj$country <- "SE Asia"
cambodia_proj$country <- "Cambodia"
indonesia_proj$country <- "Indonesia"
laos_proj$country <- "Laos"
malaysia_proj$country <- "Malaysia"
myanmar_proj$country <- "Myanmar"
philippines_proj$country <- "Philippines"
thailand_proj$country <- "Thailand"
vietnam_proj$country <- "Vietnam"

bycountry <- rbind(all_proj, cambodia_proj, indonesia_proj, laos_proj, malaysia_proj, philippines_proj,
                   thailand_proj, vietnam_proj)

bycountry$label <- bycountry$country
bycountry$label[bycountry$year != max(bycountry$year)-round((max(bycountry$year)-min(bycountry$year))/1.3,0)] <- ""

bycountry_co2 <- bycountry
bycountry_co2$capacity <- bycountry_co2$capacity * co2f
colnames(bycountry_co2)[2] <- "co2_yearly"

bycountry_co2$co2_cumulative <- bycountry_co2$co2_yearly

for (i in 2:nrow(bycountry_co2)){
  bycountry_co2$co2_cumulative[i] <- ifelse(bycountry_co2$country[i-1] == bycountry_co2$country[i],
                                            bycountry_co2$co2_cumulative[i-1] + bycountry_co2$co2_cumulative[i],
                                            bycountry_co2$co2_cumulative[i])
}

#### PARAM. SET 8 EXPORTS ####

saveRDS(projectiondf, "projectiondf_p-by-p_8.rds")
saveRDS(bycountry_co2, "co2_p-by-p_8.rds")

#### PARAM. SET 9 (HIGH LIFESPAN) ####

lifespan <- 40 # operational lifespan for individual units

projspan <- 2017:2057 # year span for projections

attrition_specificity <- 1 # 0: region-wide attrition rate, 1: country-specific

attrition_scenario <- 1 # 1: middle attrition rate; 1<: high attrition rate; 1>: low attrition rate

cap_scenario <- "mid" # "low": low capacity factor; "mid": mid capacity factor; "high": high capacity factor

operating_year <- round(mean(secoal$commission_year, na.rm = T), 0) # est. commissioning year for operating units w/o a listed commissioning year

announced_time <- 10 # pipeline times
prepermit_time <- 7
permitted_time <- 4
construction_time <- 1

#### PARAM. SET 9 PROJECTIONS ####

projspan <- paste0("Y",projspan)
lastyear <- as.numeric(substr(last(projspan), 2, 5))
firstyear <- as.numeric(substr(first(projspan), 2, 5))

attr_All <- (status_percentages$All[3] + status_percentages$All[7])/
  (status_percentages$All[3] + status_percentages$All[7] + status_percentages$All[6])*attrition_scenario
attr_Cambodia <- (status_percentages$Cambodia[3] + status_percentages$Cambodia[7])/
  (status_percentages$Cambodia[3] + status_percentages$Cambodia[7] + status_percentages$Cambodia[6])*attrition_scenario
attr_Indonesia <- (status_percentages$Indonesia[3] + status_percentages$Indonesia[7])/
  (status_percentages$Indonesia[3] + status_percentages$Indonesia[7] + status_percentages$Indonesia[6])*attrition_scenario
attr_Laos <- (status_percentages$Laos[3] + status_percentages$Laos[7])/
  (status_percentages$Laos[3] + status_percentages$Laos[7] + status_percentages$Laos[6])*attrition_scenario
attr_Malaysia <- (status_percentages$Malaysia[3] + status_percentages$Malaysia[7])/
  (status_percentages$Malaysia[3] + status_percentages$Malaysia[7] + status_percentages$Malaysia[6])*attrition_scenario
attr_Myanmar <- (status_percentages$Myanmar[3] + status_percentages$Myanmar[7])/
  (status_percentages$Myanmar[3] + status_percentages$Myanmar[7] + status_percentages$Myanmar[6])*attrition_scenario
attr_Philippines <- (status_percentages$Philippines[3] + status_percentages$Philippines[7])/
  (status_percentages$Philippines[3] + status_percentages$Philippines[7] + status_percentages$Philippines[6])*attrition_scenario
attr_Thailand <- (status_percentages$Thailand[3] + status_percentages$Thailand[7])/
  (status_percentages$Thailand[3] + status_percentages$Thailand[7] + status_percentages$Thailand[6])*attrition_scenario
attr_Vietnam <- (status_percentages$Vietnam[3] + status_percentages$Vietnam[7])/
  (status_percentages$Vietnam[3] + status_percentages$Vietnam[7] + status_percentages$Vietnam[6])*attrition_scenario

attr_All <- ifelse(attr_All > 1, 1, attr_All)
attr_Cambodia <- ifelse(attr_Cambodia > 1, 1, attr_Cambodia)
attr_Indonesia <- ifelse(attr_Indonesia > 1, 1, attr_Indonesia)
attr_Laos <- ifelse(attr_Laos > 1, 1, attr_Laos)
attr_Malaysia <- ifelse(attr_Malaysia > 1, 1, attr_Malaysia)
attr_Myanmar <- ifelse(attr_Myanmar > 1, 1, attr_Myanmar)
attr_Philippines <- ifelse(attr_Philippines > 1, 1, attr_Philippines)
attr_Thailand <- ifelse(attr_Thailand > 1, 1, attr_Thailand)
attr_Vietnam <- ifelse(attr_Vietnam > 1, 1, attr_Vietnam)

if(attrition_specificity == 0){
  secoal$capacity[secoal$status != "operating" & !is.na(secoal$capacity)] <- secoal$capacity[
    secoal$status != "operating" & !is.na(secoal$capacity)]*(1-attr_All)
} else{
  for (i in 1:nrow(secoal)){
    secoal$capacity[i] <- ifelse(secoal$country[i] == "Cambodia" &
                                   secoal$status[i] != "operating",
                                 secoal$capacity[i]*(1-attr_Cambodia),
                                 ifelse(secoal$country[i] == "Indonesia" &
                                          secoal$status[i] != "operating",
                                        secoal$capacity[i]*(1-attr_Indonesia),
                                        ifelse(secoal$country[i] == "Laos" &
                                                 secoal$status[i] != "operating",
                                               secoal$capacity[i]*(1-attr_Laos),
                                               ifelse(secoal$country[i] == "Malaysia" &
                                                        secoal$status[i] != "operating",
                                                      secoal$capacity[i]*(1-attr_Malaysia),
                                                      ifelse(secoal$country[i] == "Myanmar" &
                                                               secoal$status[i] != "operating",
                                                             secoal$capacity[i]*(1-attr_Myanmar),
                                                             ifelse(secoal$country[i] == "Philippines" &
                                                                      secoal$status[i] != "operating",
                                                                    secoal$capacity[i]*(1-attr_Philippines),
                                                                    ifelse(secoal$country[i] == "Thailand" &
                                                                             secoal$status[i] != "operating",
                                                                           secoal$capacity[i]*(1-attr_Thailand),
                                                                           ifelse(secoal$country[i] == "Vietnam" &
                                                                                    secoal$status[i] != "operating",
                                                                                  secoal$capacity[i]*(1-attr_Vietnam),
                                                                                  secoal$capacity[i]))))))))
  }
}

secoal$commission_year[secoal$status == "announced"] <- 2017 + announced_time
secoal$commission_year[secoal$status == "pre-permit"] <- 2017 + prepermit_time
secoal$commission_year[secoal$status == "permitted"] <- 2017 + permitted_time
secoal$commission_year[secoal$status == "construction"] <- 2017 + construction_time
secoal$commission_year[secoal$status == "operating" & is.na(secoal$commission_year)] <- operating_year

secoal <- secoal[!is.na(secoal$commission_year),]

secoal$retirement_year <- as.numeric(as.character(secoal$commission_year)) + lifespan

secoal[projspan] <- NA

for (j in 6:ncol(secoal)){
  .year <- as.numeric(substr(colnames(secoal)[j],2,5))
  for (i in 1:nrow(secoal)){
    secoal[i,j] <- ifelse(.year >= secoal$commission_year[i] &
                            .year <= secoal$retirement_year[i],
                          1, 0)
  }
}

secoal[,c(6:ncol(secoal))] <- sapply(secoal[,c(6:ncol(secoal))],
                                     FUN = function(x){secoal$capacity * x})
secoal[secoal$country == "Cambodia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Cambodia",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Indonesia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Indonesia",c(6:ncol(secoal))],
                                                                  FUN = function(x){capfact["Indonesia",cap_scenario] * x})
secoal[secoal$country == "Laos",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Laos",c(6:ncol(secoal))],
                                                             FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Malaysia",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Malaysia",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Malaysia",cap_scenario] * x})
secoal[secoal$country == "Myanmar",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Myanmar",c(6:ncol(secoal))],
                                                                FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Philippines",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Philippines",c(6:ncol(secoal))],
                                                                    FUN = function(x){capfact["Philippines",cap_scenario] * x})
secoal[secoal$country == "Thailand",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Thailand",c(6:ncol(secoal))],
                                                                 FUN = function(x){capfact["Mean",cap_scenario] * x})
secoal[secoal$country == "Vietnam",c(6:ncol(secoal))] <- sapply(secoal[secoal$country == "Vietnam",c(6:ncol(secoal))],
                                                                FUN = function(x){capfact["Vietnam",cap_scenario] * x})

all_proj <- data.frame(year = as.numeric(substr(projspan,2,5)), capacity = colSums(secoal[,6:ncol(secoal)]))
cambodia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Cambodia",6:ncol(secoal)]))
indonesia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                             capacity = colSums(secoal[secoal$country == "Indonesia",6:ncol(secoal)]))
laos_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                        capacity = colSums(secoal[secoal$country == "Laos",6:ncol(secoal)]))
malaysia_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Malaysia",6:ncol(secoal)]))
myanmar_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                           capacity = colSums(secoal[secoal$country == "Myanmar",6:ncol(secoal)]))
philippines_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                               capacity = colSums(secoal[secoal$country == "Philippines",6:ncol(secoal)]))
thailand_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                            capacity = colSums(secoal[secoal$country == "Thailand",6:ncol(secoal)]))
vietnam_proj <- data.frame(year = as.numeric(substr(projspan,2,5)),
                           capacity = colSums(secoal[secoal$country == "Vietnam",6:ncol(secoal)]))

projectiondf <- data.frame(Year = c(firstyear,
                                    round((lastyear - firstyear)/4, 0) + firstyear,2*round((lastyear - firstyear)/4, 0) + firstyear,3*round((lastyear - firstyear)/4, 0) + firstyear,lastyear),
                           All = c(all_proj$capacity[all_proj$year == firstyear],
                                   all_proj$capacity[all_proj$year == round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                       firstyear],
                                   all_proj$capacity[all_proj$year == lastyear]),
                           Indonesia = c(indonesia_proj$capacity[indonesia_proj$year == firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                   firstyear],
                                         indonesia_proj$capacity[indonesia_proj$year == lastyear]),
                           Malaysia = c(malaysia_proj$capacity[malaysia_proj$year == firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                 firstyear],
                                        malaysia_proj$capacity[malaysia_proj$year == lastyear]),
                           Philippines = c(philippines_proj$capacity[philippines_proj$year == firstyear],
                                           philippines_proj$capacity[philippines_proj$year == round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                                       firstyear],
                                           philippines_proj$capacity[philippines_proj$year == lastyear]),
                           Vietnam = c(vietnam_proj$capacity[vietnam_proj$year == firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == 2*round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == 3*round((lastyear - firstyear)/4, 0) +
                                                               firstyear],
                                       vietnam_proj$capacity[vietnam_proj$year == lastyear]))

projectiondf[,c(2:6)] <- round(projectiondf[,c(2:6)], 0)

countrylist <- c("All","Cambodia","Indonesia","Laos","Malaysia","Myanmar","Philippines","Thailand","Vietnam")
attrlist <- c(attr_All,attr_Cambodia,attr_Indonesia,attr_Laos,attr_Malaysia,attr_Myanmar,
              attr_Philippines,attr_Thailand,attr_Vietnam)
attrtab <- data.frame(Country = countrylist, Attrition = attrlist)
attrtab$Attrition <- round(attrtab$Attrition, 2)

co2f <- 8.2245*1000 * 215.225 * 0.00000000397347

all_proj$country <- "SE Asia"
cambodia_proj$country <- "Cambodia"
indonesia_proj$country <- "Indonesia"
laos_proj$country <- "Laos"
malaysia_proj$country <- "Malaysia"
myanmar_proj$country <- "Myanmar"
philippines_proj$country <- "Philippines"
thailand_proj$country <- "Thailand"
vietnam_proj$country <- "Vietnam"

bycountry <- rbind(all_proj, cambodia_proj, indonesia_proj, laos_proj, malaysia_proj, philippines_proj,
                   thailand_proj, vietnam_proj)

bycountry$label <- bycountry$country
bycountry$label[bycountry$year != max(bycountry$year)-round((max(bycountry$year)-min(bycountry$year))/1.3,0)] <- ""

bycountry_co2 <- bycountry
bycountry_co2$capacity <- bycountry_co2$capacity * co2f
colnames(bycountry_co2)[2] <- "co2_yearly"

bycountry_co2$co2_cumulative <- bycountry_co2$co2_yearly

for (i in 2:nrow(bycountry_co2)){
  bycountry_co2$co2_cumulative[i] <- ifelse(bycountry_co2$country[i-1] == bycountry_co2$country[i],
                                            bycountry_co2$co2_cumulative[i-1] + bycountry_co2$co2_cumulative[i],
                                            bycountry_co2$co2_cumulative[i])
}

#### PARAM. SET 9 EXPORTS ####

saveRDS(projectiondf, "projectiondf_p-by-p_9.rds")
saveRDS(bycountry_co2, "co2_p-by-p_9.rds")

############################################## #
#### **** DATA LOADING AND COMBINATION **** ####
############################################## #

rm(list=ls())

.scen1 <- "Baseline"
.scen2 <- "High Capacity Factor"
.scen3 <- "Low Capacity Factor"
.scen4 <- "80pc Attrition Rate"
.scen5 <- "120pc Attrition Rate"
.scen6 <- "30-Year Lifespan"
.scen7 <- "40-Year Lifespan"

pbp_cap_1 <- readRDS("projectiondf_p-by-p_1.rds")
pbp_cap_2 <- readRDS("projectiondf_p-by-p_2.rds")
pbp_cap_3 <- readRDS("projectiondf_p-by-p_3.rds")
pbp_cap_4 <- readRDS("projectiondf_p-by-p_4.rds")
pbp_cap_5 <- readRDS("projectiondf_p-by-p_5.rds")
pbp_cap_6 <- readRDS("projectiondf_p-by-p_8.rds")
pbp_cap_7 <- readRDS("projectiondf_p-by-p_9.rds")
pbp_cap_1$ParamVersion <- 1
pbp_cap_2$ParamVersion <- 2
pbp_cap_3$ParamVersion <- 3
pbp_cap_4$ParamVersion <- 4
pbp_cap_5$ParamVersion <- 5
pbp_cap_6$ParamVersion <- 6
pbp_cap_7$ParamVersion <- 7
pbp_cap <- bind_rows(pbp_cap_1,
                     pbp_cap_2,
                     pbp_cap_3,
                     pbp_cap_4,
                     pbp_cap_5,
                     pbp_cap_6,
                     pbp_cap_7)
pbp_cap <- pbp_cap %>%
  filter(Year == 2047) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 1, .scen1, ParamVersion)) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 2, .scen2, ParamVersion)) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 3, .scen3, ParamVersion)) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 4, .scen4, ParamVersion)) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 5, .scen5, ParamVersion)) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 6, .scen6, ParamVersion)) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 7, .scen7, ParamVersion))

pbp_co2_1 <- readRDS("co2_p-by-p_1.rds")
pbp_co2_2 <- readRDS("co2_p-by-p_2.rds")
pbp_co2_3 <- readRDS("co2_p-by-p_3.rds")
pbp_co2_4 <- readRDS("co2_p-by-p_4.rds")
pbp_co2_5 <- readRDS("co2_p-by-p_5.rds")
pbp_co2_6 <- readRDS("co2_p-by-p_8.rds")
pbp_co2_7 <- readRDS("co2_p-by-p_9.rds")
pbp_co2_1$ParamVersion <- 1
pbp_co2_2$ParamVersion <- 2
pbp_co2_3$ParamVersion <- 3
pbp_co2_4$ParamVersion <- 4
pbp_co2_5$ParamVersion <- 5
pbp_co2_6$ParamVersion <- 6
pbp_co2_7$ParamVersion <- 7
pbp_co2 <- bind_rows(pbp_co2_1,
                     pbp_co2_2,
                     pbp_co2_3,
                     pbp_co2_4,
                     pbp_co2_5,
                     pbp_co2_6,
                     pbp_co2_7)

pbp_co2_separated <- pbp_co2

pbp_co2 <- pbp_co2 %>%
  filter(year == 2047) %>%
  select(-co2_yearly, -label) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 1, .scen1, ParamVersion)) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 2, .scen2, ParamVersion)) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 3, .scen3, ParamVersion)) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 4, .scen4, ParamVersion)) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 5, .scen5, ParamVersion)) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 6, .scen6, ParamVersion)) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 7, .scen7, ParamVersion))

pbp_co2 <- pbp_co2 %>%
  dcast(year + ParamVersion + co2_cumulative ~ country, value.var = "co2_cumulative") %>%
  select(-co2_cumulative) %>%
  rename(SEAsia = "SE Asia")
pbp_co2[is.na(pbp_co2)] <- 0

pbp_co2 <- aggregate(cbind(Cambodia, Indonesia, Laos,
                           Malaysia, Philippines, SEAsia,
                           Thailand, Vietnam) ~ ParamVersion,
                     FUN = sum,
                     data = pbp_co2, na.action = na.omit)
pbp_co2_row.order <- c("Baseline","80pc Attrition Rate","120pc Attrition Rate",
                       "High Capacity Factor","Low Capacity Factor",
                       "40-Year Lifespan","30-Year Lifespan")
pbp_co2 <- pbp_co2 %>% rename("SE Asia" = SEAsia) %>% rename(Version = ParamVersion) %>%
  select(Version, "SE Asia", "Indonesia", "Vietnam", "Philippines", "Cambodia", "Laos", "Malaysia", "Thailand") %>%
  slice(match(pbp_co2_row.order, Version)) %>%
  mutate_if(is.numeric, funs(paste0(round(.,0)," (",round((. - first(.))/first(.),2)*100,"%)")))

#### Grid of plots by country, parameter ####

pbp_co2_separated <- pbp_co2_separated %>%
  select(-co2_yearly, -label) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 1, .scen1, ParamVersion)) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 2, .scen2, ParamVersion)) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 3, .scen3, ParamVersion)) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 4, .scen4, ParamVersion)) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 5, .scen5, ParamVersion)) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 6, .scen6, ParamVersion)) %>%
  mutate(ParamVersion = ifelse(ParamVersion == 7, .scen7, ParamVersion))

pbp_co2_separated[is.na(pbp_co2_separated)] <- 0

# > Capacity factor plots ####

pbp_co2_capacityfactor <- pbp_co2_separated %>% 
  filter(ParamVersion == "High Capacity Factor") %>%
  left_join(pbp_co2_separated[pbp_co2_separated$ParamVersion == "Low Capacity Factor",],
            by = c("year", "country"),
            suffix = c("_high","_low")) %>%
  left_join(pbp_co2_separated[pbp_co2_separated$ParamVersion == "Baseline",],
            by = c("year", "country"))

pbp_co2_capacityfactor %>%
  filter(country == "SE Asia") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("SE Asia") +
  xlab("Capacity Factor") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> seasia_capacityfactor

pbp_co2_capacityfactor %>%
  filter(country == "Cambodia") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Cambodia") +
  xlab("Capacity Factor") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> cambodia_capacityfactor

pbp_co2_capacityfactor %>%
  filter(country == "Indonesia") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Indonesia") +
  xlab("Capacity Factor") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> indonesia_capacityfactor

pbp_co2_capacityfactor %>%
  filter(country == "Laos") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Laos") +
  xlab("Capacity Factor") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> laos_capacityfactor

pbp_co2_capacityfactor %>%
  filter(country == "Malaysia") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Malaysia") +
  xlab("Capacity Factor") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> malaysia_capacityfactor

pbp_co2_capacityfactor %>%
  filter(country == "Philippines") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Philippines") +
  xlab("Capacity Factor") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> philippines_capacityfactor

pbp_co2_capacityfactor %>%
  filter(country == "Thailand") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Thailand") +
  xlab("Capacity Factor") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> thailand_capacityfactor

pbp_co2_capacityfactor %>%
  filter(country == "Vietnam") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Vietnam") +
  xlab("Capacity Factor") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank()) -> vietnam_capacityfactor

# > Attrition rate plots ####

pbp_co2_attritionrate <- pbp_co2_separated %>% 
  filter(ParamVersion == "80pc Attrition Rate") %>%
  left_join(pbp_co2_separated[pbp_co2_separated$ParamVersion == "120pc Attrition Rate",],
            by = c("year", "country"),
            suffix = c("_high","_low")) %>%
  left_join(pbp_co2_separated[pbp_co2_separated$ParamVersion == "Baseline",],
            by = c("year", "country"))

pbp_co2_attritionrate %>%
  filter(country == "SE Asia") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("SE Asia") +
  xlab("Attrition Rate") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> seasia_attritionrate

pbp_co2_attritionrate %>%
  filter(country == "Cambodia") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Cambodia") +
  xlab("Attrition Rate") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> cambodia_attritionrate

pbp_co2_attritionrate %>%
  filter(country == "Indonesia") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Indonesia") +
  xlab("Attrition Rate") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> indonesia_attritionrate

pbp_co2_attritionrate %>%
  filter(country == "Laos") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Laos") +
  xlab("Attrition Rate") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> laos_attritionrate

pbp_co2_attritionrate %>%
  filter(country == "Malaysia") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Malaysia") +
  xlab("Attrition Rate") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> malaysia_attritionrate

pbp_co2_attritionrate %>%
  filter(country == "Philippines") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Philippines") +
  xlab("Attrition Rate") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> philippines_attritionrate

pbp_co2_attritionrate %>%
  filter(country == "Thailand") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Thailand") +
  xlab("Attrition Rate") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> thailand_attritionrate

pbp_co2_attritionrate %>%
  filter(country == "Vietnam") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Vietnam") +
  xlab("Attrition Rate") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank()) -> vietnam_attritionrate

# > Lifespan plots ####

pbp_co2_lifespan <- pbp_co2_separated %>% 
  filter(ParamVersion == "40-Year Lifespan") %>%
  left_join(pbp_co2_separated[pbp_co2_separated$ParamVersion == "30-Year Lifespan",],
            by = c("year", "country"),
            suffix = c("_high","_low")) %>%
  left_join(pbp_co2_separated[pbp_co2_separated$ParamVersion == "Baseline",],
            by = c("year", "country"))

pbp_co2_lifespan %>%
  filter(country == "SE Asia") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("SE Asia") +
  xlab("Lifespan") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> seasia_lifespan

pbp_co2_lifespan %>%
  filter(country == "Cambodia") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Cambodia") +
  xlab("Lifespan") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> cambodia_lifespan

pbp_co2_lifespan %>%
  filter(country == "Indonesia") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Indonesia") +
  xlab("Lifespan") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> indonesia_lifespan

pbp_co2_lifespan %>%
  filter(country == "Laos") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Laos") +
  xlab("Lifespan") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> laos_lifespan

pbp_co2_lifespan %>%
  filter(country == "Malaysia") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Malaysia") +
  xlab("Lifespan") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> malaysia_lifespan

pbp_co2_lifespan %>%
  filter(country == "Philippines") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Philippines") +
  xlab("Lifespan") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> philippines_lifespan

pbp_co2_lifespan %>%
  filter(country == "Thailand") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Thailand") +
  xlab("Lifespan") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),axis.title.x = element_blank(),line = element_blank(),axis.text.x=element_blank()) -> thailand_lifespan

pbp_co2_lifespan %>%
  filter(country == "Vietnam") %>%
  ggplot(aes(x = year)) +
  geom_ribbon(aes(ymin = co2_cumulative_low, ymax = co2_cumulative_high), alpha = .2) +
  geom_line(aes(y = co2_cumulative)) +
  ylab("Vietnam") +
  xlab("Lifespan") +
  ylim(0, 16000) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank()) -> vietnam_lifespan

# > Arranging plots ####

grid.arrange(seasia_capacityfactor,
             cambodia_capacityfactor,
             indonesia_capacityfactor,
             laos_capacityfactor,
             malaysia_capacityfactor,
             philippines_capacityfactor,
             thailand_capacityfactor,
             vietnam_capacityfactor,
             ncol = 1) -> plots_capacityfactor
ggsave(plots_capacityfactor,
       filename = "plots_capacityfactor.pdf",
       units = "in", width = 2, height = 9)

grid.arrange(seasia_attritionrate,
             cambodia_attritionrate,
             indonesia_attritionrate,
             laos_attritionrate,
             malaysia_attritionrate,
             philippines_attritionrate,
             thailand_attritionrate,
             vietnam_attritionrate,
             ncol = 1) -> plots_attritionrate
ggsave(plots_attritionrate,
       filename = "plots_attritionrate.pdf",
       units = "in", width = 2, height = 9)

grid.arrange(seasia_lifespan,
             cambodia_lifespan,
             indonesia_lifespan,
             laos_lifespan,
             malaysia_lifespan,
             philippines_lifespan,
             thailand_lifespan,
             vietnam_lifespan,
             ncol = 1) -> plots_lifespan
ggsave(plots_lifespan,
       filename = "plots_lifespan.pdf",
       units = "in", width = 2, height = 9)

#### EXPORT ####

sink("pbp_co2_toptab.tex")
print(xtable(pbp_co2[,c(1:5)]), include.rownames = F, include.colnames = T, booktabs = T, floating = F)
sink()

sink("pbp_co2_bottomtab.tex")
print(xtable(pbp_co2[,c(1,6:9)]), include.rownames = F, include.colnames = T, booktabs = T, floating = F)
sink()